Week 3: Decisions and loops¶
~9 min
This week's goal¶
This week your programs start to make decisions and repeat work. You learn comparisons, if, elif, and else, the two kinds of loop (while and for), and how break and continue change a loop. By the end you can write a guessing game and print a shape made of characters.
The main text is Part 2 of the Python course, lessons 1 to 6. Work through them in order.
Day-by-day (about 1.5-2 hours per day)¶
Day 1. Read booleans and comparisons. A comparison such as 3 < 5 gives True or False. Test == and != with numbers and with strings. Write three lines in a file compare.py that print the result of comparisons you predict first. Check each prediction by running the file.
Day 2. Read if, elif, and else. Write grade.py. It asks for a score from 0 to 100 and prints a letter grade. Pick your own cut-offs, and write them in a comment at the top. Test with the edges of each range, such as 89, 90, and 100.
Day 3. Read repeating with while. Write a guessing game. The program picks a number between 1 and 20 (you can pick it yourself at first, then use the random module later), and it keeps asking the user for a guess. After each guess, it says "too high" or "too low". Stop when the guess is right, and count the guesses.
Day 4. Read repeating with for and range. Remember that range(5) gives 0, 1, 2, 3, 4, and does not include 5. Rewrite the first loop of the guessing game so it prints the numbers 1 to 10 with for and range. Then write a loop that prints the even numbers from 2 to 20.
Day 5. Read leaving a loop with break and continue. Add a break to the guessing game so that the user can type quit to stop. Then write a loop that skips multiples of 3 with continue. Print the numbers from 1 to 15 that remain.
Day 6. Read nested loops and printed patterns. Print a right triangle of stars, five rows high, using a loop inside a loop. Then print a pyramid. Each line should have the same number of characters in each row, which takes some careful thought. If you get stuck, print the row number and the column number first to see what the loops do.
Day 7 is a rest day.
Deliverable¶
Two files. guess.py plays the guessing game with while, break, and a guess counter. pattern.py prints a five-row triangle and a pyramid. Both files should run without errors when you press keys in the order the program expects.
Check yourself¶
- What does
5 == 5.0evaluate to, and why does that matter in a comparison? - In an
if,elif,elsechain, when does theelseblock run? - How is a
whileloop different from aforloop overrange? - What does
breakdo, and what doescontinuedo? - In
for i in range(1, 4), what values doesitake?
Answers
- It evaluates to
True. Python compares the numeric values, so the integer 5 and the float 5.0 are equal. This is why comparing types withtype(x) == intis a separate question from comparing values. - The
elseblock runs only when none of theiforelifconditions were true. Python checks them in order and runs the first one that matches. - A
whileloop repeats as long as its condition is true, and it can run forever if nothing changes. Aforloop overrangeruns a known number of times, once for each value in the range. breakstops the loop at once and moves on to the code after it.continueskips the rest of the current pass and goes on to the next value.- It takes 1, 2, and 3. The end value is not included.