This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
The revisions let you track differences between multiple versions of a post.
Flow control consists of tools to determine the sequence of events in a program. The primary aspects of Python flow control include:
Boolean logic - Developed by George Boole, Boolean logic has two values 0 and 1. Boolean statemments in Python will return True or False.
Loops - a loop is a piece of code that is written only once and run many times until a condition is satisfied to end the loop.
Iterators - Iterators allow a program to execute the same code on every element of a group (e.g. check to see if all items in a shopping list are in the shopping cart)
Exercise
Exercise 5.2
http://www.pastie.org/1964328
Final answer?
Final answer? :-)
# Exercise 5.2 totalVal =
# Exercise 5.2 totalVal = 0 valCount = 0 smallestVal = '' biggestVal = '' while True: val = raw_input('Enter a number: ') if val != 'done': try: val = float(val) except: print 'Bad data' continue if val != 'done': totalVal += val valCount += 1 if smallestVal == '' or biggestVal == '': smallestVal = val biggestVal = val if val > biggestVal: biggestVal = val if val < smallestVal: smallestVal = val else: print 'totalVal: ', totalVal print 'valCount: ', valCount print 'biggest value: ', biggestVal print 'smallest value: ', smallestVal breakQ: What are some physical
Q: What are some physical systems that we see in every day life that exhibit flow control?
A: Opening and closing times of businesses, schools, etc.
------------------
# Exercise 5.1 totalVal = 0 valCount = 0 while True: val = raw_input('Enter a number: ') if val != 'done': try: float(val) except: print 'Bad data' continue if val == 'done': print 'totalVal: ', totalVal print 'valCount: ', valCount print 'valAverage: ', valAvg break else: totalVal += float(val) valCount += 1 valAvg = totalVal / valCount# Excercise 3.1 try:
# Excercise 3.1, 3.2 try: hours = float(raw_input('Enter Hours: ')) except: print 'Please enter numeric values only.' try: rate = float(raw_input('Enter Rate: ')) except: print 'Please enter numeric values only.' overtimeThreshold = 40 overtimeGearing = 1.5 maxHours = 90 try: print 'Pay: ', if hours <= overtimeThreshold: print hours * rate elif hours <= maxHours: print (overtimeThreshold * rate) + (hours - overtimeThreshold) * (rate * overtimeGearing) else: print 'Too many hours.' except: print 'Unable to calculate pay.'