Chapter 5
If and loops
Programs make choices with if, elif and else, and repeat work with for and while loops. This chapter covers range() and its steps, running totals, which branch of an if wins, when a while loop stops, break and continue, and pass. It ends with the two mistakes every beginner makes: forgetting to indent, and writing = when you mean ==.
11 cards
See the 11 cards
- What does range(3) give as a list?[0, 1, 2]
- What does range with a step of 3 give?[1, 4, 7]
- What does the running total print?6
- Which branch prints for x = 15?medium
- When does the doubling loop stop?32
- What does i hold after the break?3
- What's wrong here: the line after the if?IndentationError
- What's wrong here: the test in the if?SyntaxError
- Fill the blank so the loop prints 0, 1, 2i
- Keyword that skips to a loop's next passcontinue
- Keyword that does nothing, as a placeholderpass
Image credits
- What does range(3) give as a list?: Brain Zap, CC0 1.0 Source
- What does range with a step of 3 give?: Brain Zap, CC0 1.0 Source
- What does the running total print?: Brain Zap, CC0 1.0 Source
- Which branch prints for x = 15?: Brain Zap, CC0 1.0 Source
- When does the doubling loop stop?: Brain Zap, CC0 1.0 Source
- What does i hold after the break?: Brain Zap, CC0 1.0 Source
- What's wrong here: the line after the if?: Brain Zap, CC0 1.0 Source
- What's wrong here: the test in the if?: Brain Zap, CC0 1.0 Source
- Fill the blank so the loop prints 0, 1, 2: Brain Zap, CC0 1.0 Source