Graduate Exercise in python language

Graduate Exercise in python language
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
Graduate Exercise in python language


Graduate Exercise
Write a program, graduate.py, that prompts students for how many credits they have. Print whether or not they have enough credits for graduation. (At Loyola University Chicago 120 credits are needed for graduation.)


 Head or Tails Exercise
Write a program headstails.py. It should include a function flip(), that simulates a single flip of a coin: It randomly prints either Heads or Tails. Accomplish this by choosing 0 or 1 arbitrarily with random.randrange(2), and use an if-else statement to print Heads when the result is 0, and Tailsotherwise.

In your main program have a simple repeat loop that calls flip() 10 times to test it, so you generate a random sequence of 10 Heads and Tails.

Head or Tails Exercise














 Strange Function Exercise
Save the example program jumpFuncStub.py as jumpFunc.py, and complete the definitions of functions jump and main as described in the function documentation strings in the program. In the jumpfunction definition uses an if-else statement (hint [2]). In the main function, definition uses a for-each loop, the range function, and the jump function.
The jump function is introduced for use in Strange Sequence Exercise, and others after that.
Strange Function Exercise

























 Multiple Tests and if-elif Statements
Often you want to distinguish between more than two distinct cases, but conditions only have two possible results, True or False, so the only direct choice is between two options. As anyone who has played “20 Questions” knows, you can distinguish more cases with further questions. If there are more than two choices, a single test may only reduce the possibilities, but further tests can reduce the possibilities further and further. Since most any kind of statement can be placed in an indented statement block, one choice is further if statement. For instance consider a function to convert a numerical grade to a letter grade, ‘A’, ‘B’, ‘C’, ‘D’ or ‘F’, where the cutoffs for ‘A’, ‘B’, ‘C’, and ‘D’ are 90, 80, 70, and 60 respectively
Multiple Tests and if-elif Statements




















One way to write the function would be tested for one grade at a time, and resolve all the remaining possibilities inside the next else clause:
def letterGrade(score):
    if score >= 90:
        letter = 'A'
    else:   # grade must be B, C, D or F
        if score >= 80:
            letter = 'B'
        else# grade must be C, D or F
            if score >= 70:
                letter = 'C'
            else:    # grade must D or F
                if score >= 60:
                    letter = 'D'
                else:
                    letter = 'F'
    return letter

This repeatedly increasing indentation with an if statement as the else block can be annoying and distracting. A preferred alternative in this situation, that avoids all this indentation, is to combine each else and if block into an elif block:
def letterGrade(score):
    if score >= 90:
        letter = 'A'
    elif score >= 80:
        letter = 'B'
    elif score >= 70:
        letter = 'C'
    elif score >= 60:
        letter = 'D'
    else:
        letter = 'F'
    return letter
The most elaborate syntax for an if-elif-else statement is indicated in general below:
if condition1 :
indentedStatementBlockForTrueCondition1
elif condition2 :
indentedStatementBlockForFirstTrueCondition2
elif condition3 :
indentedStatementBlockForFirstTrueCondition3
elif condition4 :
indentedStatementBlockForFirstTrueCondition4
else:

indentedStatementBlockForEachConditionFalse
The if, each elif, and the final else line are all aligned. There can be any number of elif lines, each followed by an indented block. (Three happen to be illustrated above.) With this 
construction exactly one of the indented blocks is executed. It is the one corresponding to the first True condition, or, if all conditions are False, it is the block after the final else line.
Be careful of the strange Python contraction. It is elif, not elseif. A program testing the letterGrade function is an example program grade1.py.
A final alternative for if statements: if-elif-.... with no else. This would mean changing the syntax for if-elif-else above so the final else: and the block after it would be omitted. It is similar to the basic if statement without an else, in that it is possible for no indented block to be executed. This happens if none of the conditions in the tests are true.
With an else included, exactly one of the indented blocks is executed. Without an else, at most one of the indented blocks is executed.

if weight > 120:
    print('Sorry, we can not take a suitcase that heavy.')
elif weight > 50:
    print('There is a $25 charge for luggage that heavy.')
This if-elif statement only prints a line if there is a problem with the weight of the suitcase











































Comments