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


  • Fer said:

    Chapter 3 exercises


    http://pastebin.com/jQCJW5TY

     

    Chapter 5 exercises

    http://pastebin.com/enT9eak2

    on Nov. 6, 2013, 9:59 a.m.
  • Fer said:

    Everyday systems relying on flow control.....

    Excess of speed activated traffic light system

    Overweight alarm system on elevators

     

    Anything that can't be counted/measured is out of real reach for a True/False

    Is it warm enough? (for whom??)

     

     

    on Nov. 6, 2013, 6:54 a.m.
  • Wouter Tebbens said:

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

    a few of the examples around me:

    • a thermostat that switches on and off depending on the temperature,
    • a timer switches the light off when 5 minutes have gone by,
    • an alarm clock rings its alarm when a selected time is reached,
    • a babyphone, that transmits a baby's sound when that is higher than a certain threshold, and gets silent again when it gets below the threshold

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

    multiple choice, natural language, the level of light in the room (that can change over a continuum)

    on March 22, 2013, 2:45 p.m.

    Anonym said:

    I am away for a short period. I will endeavour to respond to your email when I return. If you don't hear from me in a couple of weeks or so, please send me a reminder.
    on March 22, 2013, 2:48 p.m. in reply to Wouter Tebbens
  • drediamond said:

    Refridgerators exhibit flow control!

    on Nov. 27, 2012, 8:23 p.m.

    cstsupport said:

    Greetings from Ireland. Thank you for contacting me. I am are currently out of the office and will have some access to my email. I will return your email and attend to queries as soon as I can. Warm Regards, Rosetta Talley
    on Nov. 27, 2012, 8:25 p.m. in reply to drediamond
  • saravanan said:

    Chapter 3 and Chapter 5

    completed

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

    Dams, ethernet, temperature control...

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

    multiple selections situations. purchasing things etc,.

    on Sept. 7, 2012, 3:37 a.m.
  • Luisa said:

    I'm trying to solve the following exercise and have written the code for it. But somehow, I'm getting a syntax error at the first line. I'm not sure why. Could someone help me with this? Thanks!

    Exercise: Write a script that prompts the user for their name. The script should then print the lyrics to the song below, giving the user’s name a new first letter in each line. If the first letter of the original name is a consonant, it should be deleted and replaced by the new letter. Otherwise, the new letter should be inserted before the first letter of the name.

    Enter your name: Paul 
    Paul Paul Bo Baul 
    Banana Fanna Fo Faul 
    Me My Mo Maul 
    Paul

    Enter your name: Ann
    Ann Ann Bo BAnn 
    Banana Fanna Fo FAnn 
    Me My Mo MAnn 
    Ann

     

    My Solution: 

    n=input("Enter your name: ")

    If n[0] in ('a','e','i','o','u'):

    print (n, n, 'Bo', B+n[0:])

    print ('Banana', 'Fanna', 'Fo', 'F'+n[0:])

    print ('Me', 'My', 'Mo', 'M'+n[0:])

    print (n)

    Else:

    print (n,n, 'Bo', B+n[1:])

    print ('Banana', 'Fanna', 'Fo', 'F'+n[1:])

    print ('Me', 'My', 'Mo', 'M'+n[1:])

    print (n)

    on Sept. 6, 2012, 7:42 p.m.

    saravanan said:

    I tried your code,

    i found the error in the line

    print (n, n, 'Bo', B+n[0:]) --- change ---- B ---> 'B'  in both if and else statement

    B is a string.

    on Sept. 7, 2012, 10:02 a.m. in reply to Luisa

    Luisa said:

    Thanks Saravanan. Sadly, there still seems to be something wrong with it. It won't run in IDLE because there is still a Syntax error, but I can't seem to giure out what it is. IDLE highlights the n[0] in line 2 but I'm not sure what's wrong with it. Thanks!

    on Sept. 7, 2012, 5:17 p.m. in reply to saravanan

    Shannon said:

    If the above is literally your code, make 'If' and 'Else' lowercase (and put quotes around the string 'B', as saravanan advised). Success?

    on Sept. 7, 2012, 7:12 p.m. in reply to Luisa

    Luisa said:

    Sadly, not yet. This is the message that I get:

     

    Traceback (most recent call last):
      File "/Users/emmibeck/Desktop/ex2", line 1, in <module>
        n=input("Enter your name: ")
      File "<string>", line 1, in <module>
     
    Thanks for your help!
    on Sept. 7, 2012, 7:30 p.m. in reply to Shannon

    Shannon said:

    With the corrections mentioned, it worked in IDLE for me. Are you using python 3.xxxx? On Fri, Sep 7, 2012 at 7:31 PM, Luisa <
    on Sept. 7, 2012, 8:01 p.m. in reply to Luisa

    saravanan said:

    the code worked for me too in idle 3.  The error she mentioned used to occur in python 2.7.3  because of using input() instead of raw_input().

    on Sept. 7, 2012, 11:40 p.m. in reply to Shannon

    Luisa said:

    Thanks so much! You're right, I was using 2.7. Now I 've downloaded 3.2.3 but it keeps freezing and crashing. Have you had this problem before? It tells me " WARNING: The version of Tcl/Tk (8.5.7) in use may be unstable."

     
    Thanks again!
    on Sept. 11, 2012, 12:28 a.m. in reply to Shannon

    Shannon said:

    Er, you're welcome. Sorry to complicate things. In googling your error message, I gather you're using OSX. I'd suggest looking at the first task to find someone with your OS version who installed Python 3 successfully and getting things straight. I don't use OSX, haven't had this problem, and don't know what I'm talking about.

    BUT, as I read it, you have 3 options:

    1 - Uninstall Python 3 and re-install Python 2, which as you've seen has syntax differences.

    2- Install and use a different IDE rather than IDLE. If choosing this, I would look at the first task and read what OSXers prefer.

    3- Download an updated Tcl/Tk. See the chart atop http://www.python.org/download/mac/tcltk/  to see the version you need. I'm guessing you want 8.5.12 from http://www.activestate.com/activetcl/downloads

    Good luck!

    on Sept. 11, 2012, 8:55 a.m. in reply to Luisa
  • Rob said:

    Chapters 3 and 5:  http://pastebin.com/DwPNweaE

    on Aug. 25, 2012, 7:47 p.m.
  • ionut.vlasin said:

    on July 10, 2012, 12:03 p.m.
  • qiu6406 said:

    on May 11, 2012, 10:27 a.m.
  • pannix said:

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

    Form validation, temperature control, access permissions , ...

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

    Multiple choice, grading (exams), working with intervals, ...

    on May 10, 2012, 3:17 p.m.
  • dvdjaco said:

    Q: What are some everyday systems that exhibit flow control?
        Cars (ie engine warning signals), ground fault interrupters, heating/AC thermostats

    Q: Can you think of some examples where True/False logic may be too constraining?
        - When working with magnitudes that change in a continuous way (eg temperature), and that fact needs to be taken into account in the program.
        - When working with natural language.

    on Feb. 21, 2012, 6:17 p.m.
  • Ken Doman said:

    My answers for Chapter 3:

    Chapter 3 Exercises

    My answers for Chapter 5:

    Chapter 5 Exercises

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

    Toll booths (for cars in lane)

    Heating/Air Conditioning systems (while temperature < set_temperature: heat())

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

    When you need to use the contents of a string for something bigger than (string == "something")

    on Jan. 17, 2012, 1:25 p.m.
  • Evita said:

    Chapter 3 - exercises:

    http://pastebin.com/d7JnEf3X

    Chapter 5 - exercises:

    http://pastebin.com/8W0GV16q

     

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

    A: Production systems, bank systems.

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

    A: When having more that one choise available.

    on Oct. 10, 2011, 1:53 p.m.
  • Mars83 said:

    My Python (3.x) source codes:

     

    on Oct. 4, 2011, 11:44 a.m.
  • dean said:

    My answers:

    http://goo.gl/Rmuxi

    on Oct. 2, 2011, 1:11 p.m.
  • NamKeph said:

    My exercises:

    chapter 3

    http://pastebin.com/emBPWDra

    chapter 5

    http://pastebin.com/HScgJNLB

     

    The answers to the questions:

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

    • pipeline systems

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

    • multiple choice cases where the choice is between more than 2 options
    on Sept. 26, 2011, 7:20 a.m.
  • Sage Ross said:

    I've been doing each exercise as a function, and combining related ones where it makes sense to do so.  Here's what I've got so far:

    http://pastebin.com/YN1F85iA

    on Sept. 22, 2011, 10:03 p.m.
  • emilfos said:

    Here is my code.

    #Edit: Forgot to put the code here.

     

    http://pastebin.com/PExr0TWD

    on Sept. 15, 2011, 9:39 p.m.
  • EmperiuM said:

    Exercise 3.2 

     

    http://pastebin.com/1Rwm0sSq

    on July 15, 2011, 5:23 a.m.