This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Flow Control



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

Please review the Flow Control learning resources. Post answers to the practice exercises below.

Q: What are some everyday systems that exhibit flow control?

Q: Can you think of some examples where True/False logic may be too constraining?

Learning Resources

Task Discussion


  • 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
    
    

    on June 25, 2011, 5:14 p.m.
  • Anonym said:

     

     

    https://github.com/DamionKing/P2PU

    Assignment style take from Tyler

    on June 19, 2011, 4:36 p.m.
  • Josue said:

    Here is my code on gitHub for exercises 3.1 and 3.2

    The link for code for exercise 3.3 is HERE

    Q: What are some everyday systems that exhibit flow control?

    Traffic control, controling an assembly line

    Q: Can you think of some examples where True/False logic may be too constraining?

    any operation that involves selecting more than 1 option, like a multiple selection question on an exam, multiple operations to be done to a motor, car on an assembly line

    on May 25, 2011, 2:28 a.m.
  • Vladimir Támara Patiño said:

    print "5.1"
    count = 0
    sum = 0
    while True:
        n = raw_input("Enter a number: ")
        if n == 'done':
            break
        try:
            nint = int(n)
            count = count + 1
            sum = sum + nint
        except:
            print "Invalid input"
    av = 0.0
    if count > 0:
        av = float(sum)/float(count)
    print sum, count, av
    
    
    print "5.2"
    count = 0
    min = None
    max = None
    sum = 0
    while True:
        n = raw_input("Enter a number: ")
        if n == 'done':
            break
        try:
            nint = int(n)
            count = count + 1
            sum = sum + nint
            if min is None or nint < min:
                min = nint
            if max is None or nint > max:
                max = nint
        except:
            print "Invalid input"
    print sum, count, min, max
    

     

    Problems in chapter 3:

    Page 41. in the list of comparision operators, the operators "is" and "is not" are missing. The same happens in slides of chapter 3

     

    Other information that could be introduced:

    Comparision operators "in" and "not in":

    With them all the comparision operators of python are covered, they can be used with strings to decide if one is substring of the other, for example:

    "dict" in "dictionary" 
    

    is true, while

    "pict" in "dictionary" 
    

    is false

    The difference betwwen == and "is":

    With the types introduced up to now (int, float, str, bool) both operands are equivalent if the two operands have the same type.

    If the operands have different type "is" will return false always.

    3 is 3.0 False

    If both operands are numeric "==" will convert to float and compare.

    3 == 3.0 True

    If one of the operands is numeric (int or float) and the other is Boolean it will convert the number to boolean and compare (0 and 0.0 are false, other numbers are True).

    1 == True becomes True

    0 == True becomes False

    0 == False becomes True

    0.0 == False becomes True

    This behavior between numeric types and booleans with "==" is not specified in the Python Language Reference http://docs.python.org/reference/expressions.html#notin that regardings == and non-numeric types says "Otherwise, objects of different types always compare unequal"

    False is less than True

    on May 18, 2011, 8:02 a.m.
  • Vladimir Támara Patiño said:

    print "3.1"
    hours = float(raw_input('Enter Hours: '))
    rate = float(raw_input('Enter Rate: '))
    pay = hours*rate
    if (hours>40.0):
        pay = pay + (hours-40)*rate*0.5
    print "Pay", pay
    
    
    print "3.2"
    try:
        hours = float(raw_input('Enter Hours: '))
        rate = float(raw_input('Enter Rate: '))
        pay = hours*rate
        if (hours>40.0):
            pay = pay + (hours-40)*rate*0.5
        print "Pay", pay
    except:
        print "Error, please enter numeric input"
    
    
    print "3.3"
    try:
        s = float(raw_input('Enter score: '))
        if (s > 1.0 or s<0.0):
            print 'Bad score'
        elif (s >= 0.9):
            print 'A'
        elif (s >= 0.8):
            print 'B'
        elif (s >= 0.7):
            print 'C'
        elif (s >= 0.6):
            print 'D'
        else:
            print 'F'
    except:
        print 'Bad score'
    
    on May 16, 2011, 11:44 p.m.
  • Todd Hayes said:

    Exercise 3.3 - Flow Control, on Pastie:

    http://pastie.org/1912759

    on May 16, 2011, 4:51 p.m.
  • Todd Hayes said:

    Exercise 3.2 - Flow Control, on Pastie:

    http://pastie.org/1912720

    on May 16, 2011, 4:44 p.m.
  • Todd Hayes said:

    Q: What are some everyday systems that exhibit flow control?

    Heating and cooling, traffic/racing, electronics, restaurants

    Q: Can you think of some examples where True/False logic may be too constraining?

    Multiple selection criteria (choice of color for a new car on website, grading system with curve, online surveys)

    on May 16, 2011, 4:38 p.m.
  • Todd Hayes said:

    Exercise 3.1 for Flow Control, on Pastie:

    http://pastie.org/1912633

    on May 16, 2011, 4:24 p.m.