Sudaraka Wijesinghe said:
Q: What are some everyday systems that exhibit flow control?
Menu driven devices like cell phones, DVD players, ATMs.
Q: Can you think of some examples where True/False logic may be too constraining?
Evaluation of a value or expression that can have more than two states, i.e selecting an item from a list (menu).
My py4int exercise 3 code
Exercise 3.1 and 3.2
try: # Get hours from the user hours = raw_input('Enter number of ours: ') f_hours = float(hours) # Get rate from the user rate = raw_input('Enter rate per hour: ') f_rate = float(rate) except: print 'Error, please enter numeric input' quit() # Calculate pay/bonus if f_hours > 40 : pay = f_rate * 40 + f_rate * 1.5 * (f_hours - 40) else: pay = f_hours * f_rate # Output calculated pay print 'Gross pay:',pay
Exercise 3.3
# Get score from user inp = raw_input('Enter score: ') try: score = float(inp) except: print 'Bad score' quit() # Make sure score is less than one if 1 < score or 0 > score : print 'Bad score' quit() if 0.9 < score : print 'A' elif 0.8 < score : print 'B' elif 0.7 < score : print 'C' elif 0.6 < score : print 'D' else: print 'F'
My py4int exercise 5 code
Exercise 5.1
# Initialize variables count = 0 total = 0.0 while 1 : inp = raw_input ('Enter a number: ') if 'done' == inp : break try: total = total + float(inp) # increase count after total to avoid incorrect count increases on bad inputs count = count + 1 except: print 'Invalid input' # Avoid zero division if 0 < count : print 'Total:', total, ' Count:', count, ' Average', total/count else: print 'No numbers entered'
Exercise 5.2
# Initialize variables max_val = None min_val = None while 1 : inp = raw_input ('Enter a number: ') if 'done' == inp : break try: value = float(inp) # Check for minimum if min_val is None : min_val = value elif min_val > value : min_val = value # Check for maximum if max_val is None : max_val = value elif max_val < value : max_val = value except: print 'Invalid input' print 'Maximum:', max_val, ', Minimum:', min_val