Decisions and Loops
Teach your programs to choose and to repeat. You'll compare values, make decisions with if, elif and else, combine conditions with and, or and not, and repeat work with while and for loops, counting with range. Then you'll price a water park's tickets for a whole family, and finish Module 1 ready for its quiz game project.
Updated
Conditions in Python
- True, False and Comparisons (Novice): A comparison such as height >= 48 asks Python a yes-or-no question, and its answer is a Boolean value, True or False, the value every decision a program makes is built on.
- Making Decisions with if, elif and else (Novice): An if statement runs its indented block only when its condition is True, elif tries the next condition when the ones above it were False, and else catches every case that's left, so a program takes the right path for each situation.
- Combining Conditions with and, or and not (Apprentice): and is True only when both conditions are True, or is True when at least one is, not flips True and False, and in checks whether text appears inside other text, which together turn a real rule with several parts into one condition.
Loops in Python
- Repeating with while (Novice): A while loop runs its indented block again and again as long as its condition stays True, so a program can count, keep going until a job is done, or keep asking until an answer makes sense.
- Counting with for and range (Novice): A for loop runs its block once for each item it's handed, range() hands it a row of numbers to count with, up, down or in steps, and break and continue end a loop early or skip one pass.
Exercise: Water Park Tickets
- Exercise: Water Park Tickets (Apprentice): Finish a water park's ticket script that checks the group's size with a while loop, prices each person's ticket by age and day with if, elif, else and and, and totals the group with a for loop over range, while pytest types the answers in and checks every line.
Up Next: Functions and Data
- Up Next: Functions and Data (Novice): Look back at what Module 1 taught you, put all of it to work in a quiz game of your own, and get a first look at the functions, lists and dictionaries that Module 2 teaches.