This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Python Programming 101

Revision of Flow Control from Mon, 2011-05-02 08:09

The revisions let you track differences between multiple versions of a post.

Brylie Oxley's picture
Mon, 2011-05-02 08:09

Flow control consists of tools to determine the sequence of events in a program. The primary aspects of Python flow control include:

  • boolean logic
  • loops
  • iterators

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)

Learning Resources

Final answer?

Brylie Oxley's picture
Brylie Oxley
Wed, 2011-05-18 04:03

Final answer? :-)

# Exercise 5.2 totalVal =

Luc Chase's picture
Luc Chase
Wed, 2011-05-04 15:48
# 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
        break

Q: What are some physical

Luc Chase's picture
Luc Chase
Wed, 2011-05-04 15:04

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:

Luc Chase's picture
Luc Chase
Wed, 2011-05-04 13:50
# 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.'