
randrange ( xLow, xHigh + 1 ) y = random. sleep ( delay ) def getRandomPoint ( xLow, xHigh, yLow, yHigh ): '''Return a random Point with coordinates in the range specified.''' x = random. getY () if x xHigh : dx = - dx if y yHigh : dy = - dy time. ''' from graphics import * import time, random def bounceInBox ( shape, dx, dy, xLow, xHigh, yLow, yHigh ): ''' Animate a shape moving in jumps (dx, dy), bouncing when its center reaches the low and high x and y coordinates. ''' Show a ball bouncing off the sides of the window. Without an else, at most one of the indented blocks With an else included, exactly one of the indented blocks isĮxecuted. This happens if none of the conditions in the Without an else, in that it is possible for no indented block If- elif- else above so the final else: and the block after it A program testing the letterGrade function is inĪ final alternative for if statements: if- elif. It is the one corresponding to the first TrueĬondition, or, if all conditions are False, it is the blockīe careful of the strange Python contraction. This construction exactly one of the indented blocks isĮxecuted. (Three happen to be illustrated above.) With There can be any number of elif lines, each followedīy an indented block. The if, each elif, and the final else lines are allĪligned. IndentedStatementBlockForEachConditionFalse There are generally a number of ways you might

General calculation formula and sets the parameters for the formula So the conversion from string was via float, not int.īelow is an equivalent alternative version of the body ofĬalcWeeklyWages, used in wages1.py. Here the input was intended to be numeric, but it could be decimal ''' if totalHours <= 40 : totalWages = hourlyWage * totalHours else : overtime = totalHours - 40 totalWages = hourlyWage * 40 + ( 1.5 * hourlyWage ) * overtime return totalWages def main (): hours = float ( input ( 'Enter hours worked: ' )) wage = float ( input ( 'Enter dollars paid per hour: ' )) total = calcWeeklyWages ( hours, wage ) print ( 'Wages for.

It is always executed in the normal forward flow of statements,Īfter the if- else statement (whichever block is selected).ĭef calcWeeklyWages ( totalHours, hourlyWage ): '''Return the total weekly wages for a worker working totalHours, with a given regular hourlyWage. Since its amount of indentation matches the if heading, Since it is dedented, it is not a part of the if-else statement: Removing indentation, about getting exercise. Statement exactly one of two possible indented blocks is executed. In the if- else form this is followed by anĮlse: line, followed by another indented block that is onlyĮxecuted when the original condition is false. If heading and is executed when the condition in the if One, like in the simple if statement, comes right after the Again it isĬlose to English, though you might say “otherwise” instead of The middle four lines are an if-else statement. Temperature = float ( input ( 'What is the temperature? ' )) if temperature > 70 : print ( 'Wear shorts.' ) else : print ( 'Wear long pants.' ) print ( 'Get some exercise outside.' )
