Instructions
The information on this page is not stored. If you start an assignment, you must finish it. This page does not save your progress.
Quick review
range()
range(5) # 0 1 2 3 4 range(1, 6) # 1 2 3 4 5 range(2, 11, 2) # 2 4 6 8 10 range(5, 0, -1) # 5 4 3 2 1
Looping through sequences
for letter in "hey":
print(letter)
pets = ["dog", "cat"]
for pet in pets:
print(pet)
The in operator
"a" in "cat" # True "kiwi" in ["kiwi", "fig"] # True "ki" in ["kiwi", "fig"] # False "x" not in "cat" # True
- A
forloop repeats once for every item in a sequence. The loop variable holds the next item each time. - In
range(start, stop, step), start is in, stop is out. The default start is 0 and the default step is 1. To include the last number, add 1 to the stop:range(1, 11)gives 1 to 10. - To count down, use a negative step and a stop that is smaller than the start:
range(10, 0, -1). - A string loop visits one character at a time (spaces count too). A list loop visits one whole item at a time. Lists use square brackets and commas:
["apple", "kiwi"]. inanswers True or False. In a string it can find a letter or a chunk. In a list it needs a whole item. It is case sensitive.print(x, end=" ")keeps the output on the same line. A plainprint()ends the line.
Error messages you may see in the terminal
SyntaxError: expected ':'means theforline is missing its colon.IndentationError: expected an indented block after 'for' statement on line 3means the body is not indented 4 spaces.TypeError: 'int' object is not iterablemeans you triedfor i in 5:. Userange(5).ValueError: range() arg 3 must not be zeromeans the step is 0.TypeError: 'in <string>' requires string as left operand, not intmeans you checked for a number inside a string. Put quotes around it.- No error at all is possible too! Off-by-one ranges and a countdown with a positive step run without an error but give the wrong output. Compare your output with the expected output.
How this works
- Enter your first name, last name initial, and class period, then click Start Assignment. Your start time is recorded.
- The assignment opens in a Python editor with four files (tabs). Each orange numbered box or drop down is one practice item. There are 20 items.
- Click ▶ Run on Parts 1 to 3 to see what Python prints. Compare it with the expected output. Run is turned off for Part 4. Run does not grade you, so check your work carefully.
- When you finish all four tabs, click Submit for Score. Your end time is recorded and your score appears. Answers cannot be changed after submitting.
| Part | Task | Items | Points |
|---|---|---|---|
| 1 | range(): repeat, count up, count by steps, count down, and write a whole for line | 1 to 5 | 5 |
| 2 | Looping through strings and lists, the accumulator, and end= | 6 to 10 | 5 |
| 3 | The in operator: inside a loop, in a list, and fixing in bugs | 11 to 15 | 5 |
| 4 | Predict the output and choose for or while | 16 to 20 | 5 |
| Total (1 point per item) | 20 | 20 |
Student Information
Items answered: 0 of 20
Task: Each program has a comment that says what it should print. Fill in each numbered box so the
range() makes exactly the right numbers.
- Remember: start is in, stop is out.
- Item 5: write the whole
forline yourself. The body uses the loop variablen.
Hip hip hooray!
Hip hip hooray!
Hip hip hooray!
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
10 9 8 7 6 5 4 3 2 1
Liftoff!
0 5 10 15 20 25 30 35 40 45 50
Task: Loop through strings and lists. Complete every numbered box and drop down.
- Item 7: type the whole list, with square brackets, quotes, and commas.
- Item 10: type what goes after the comma inside
print().
P
y
t
h
o
n
I like apple
I like mango
I like kiwi
Feed the dog
Feed the cat
Feed the fish
Total: 255
*****
Task: Use
in and not in to test whether something is inside a string or a list.
- Item 13: write the whole condition yourself. Use the variables, not the names typed out.
- Items 14 and 15 already have code in them, but the code has a bug. Fix it.
Vowels: 4
Hithere
Leo is on the list
We have kiwi
Code has a 7
Task: Trace each small program in your head and choose what it prints.
- Items 16 to 19: write out each range or sequence on paper before you choose.
- Item 20: decide which loop fits the job best.
TerminalProblemsOutput