This assignment has not been submitted. Click Submit for Score first, then click Save as PDF on the results.
Instructions
The information on this page is not stored. If you start an assignment, you must finish it. To get a grade, you must save your results as a PDF and turn it in (steps are below).
Quick review
An if / else inside a while loop
n = 1
while n <= 4:
if n % 2 == 0: # 4 spaces
print(n, "even") # 8 spaces
else:
print(n, "odd")
n += 1 # back to 4 spaces
Count only some values
count = 0
n = 1
while n <= 20:
if n % 5 == 0:
count += 1 # only when True
n += 1 # every time
print(count) # 4
Keep asking with and / or
age = int(input("Age: "))
while age < 1 or age > 120:
print("Not a real age.")
age = int(input("Age: "))
print("Thanks!")
- The
whilebody is indented 4 spaces. The body of anifinside the loop is indented 8 spaces. The indentation tells Python which lines belong to which block. - The update (like
n += 1) must run every time through the loop, so it lines up with theif(4 spaces), not inside it. If it only runs when theifis True, the loop can get stuck forever. %gives the remainder.n % 2 == 0means n is even.n % 3 == 0means n is a multiple of 3.andis True only when both sides are True.oris True when at least one side is True.notflips True and False.- Each side of
and/ormust be a full comparison: writex == "yes" or x == "y", notx == "yes" or "y". - Python checks
if/eliffrom the top and runs only the first True branch.elsenever has a condition.
Messages you may see in the terminal
Stopped: this loop ran 1000 times...means an infinite loop: the condition never becomes False. Check that the update is indented to line up with theif(not inside it), and that it moves the right way. (Real Python keeps going until you press Ctrl + C, which showsKeyboardInterrupt.)Stopped: the program asked for more input than the user typedmeans a loop that asks withinput()kept going after it should have stopped. Check the loop condition.SyntaxError: invalid syntax. Maybe you meant '==' ...means a single=was used in a condition. Use==to compare.SyntaxError: expected ':'means awhile,if,elif, orelseline is missing its colon, or anelsehas a condition after it.- No error at all is possible too! A wrong operator or a wrong
and/orjust prints the wrong thing. 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 file holds several small programs. Each orange numbered box or drop down is one item. There are 20 items.
- Click ▶ Run to run every program in that file. Programs that use
input()get their answers typed for you (the comment says what the user types). Run does not grade you, so compare with the expected output. - 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.
Turning it in: save a PDF (required for a grade)
- After you click Submit for Score, click the Save as PDF button at the bottom of your results.
- In the print window, set Destination to Save as PDF (not a printer), then click Save.
- Keep the file name it suggests. It already has your name and period, for example
While_If_Maria_R_P3.pdf. - Turn in the PDF where your teacher tells you to.
Do not refresh or close the page until your PDF is saved. Nothing is stored, so if you leave first you will have to do the assignment again. The PDF shows your name, period, times, score, every answer, and your completed code.
| Part | Task | Items | Points |
|---|---|---|---|
| 1 | Decisions inside a loop: if / elif / else and % | 1 to 5 | 5 |
| 2 | Counters, totals, and input() checks with and / or | 6 to 10 | 5 |
| 3 | Debug: = vs ==, a stuck update, or mistakes, else with a condition, and off-by-one ranges | 11 to 15 | 5 |
| 4 | Predict what each loop does | 16 to 20 | 5 |
| Total (1 point per item) | 20 | 20 |
Student Information
Items answered: 0 of 20
Task: Each loop makes a decision every time it repeats. Complete each program so it prints what its comment says.
- Item 3: type the whole condition.
- Item 4: type the whole
elifline. The 4 spaces in front are already there. - Item 5: type the whole update line, including the spaces in front. Think about which block it belongs to.
Program A: 1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
Program B: 1 2 Clap 4 5 Clap 7 8 Clap
Program C: 60 Cool
70 Cool
80 Warm
90 Hot
100 Hot
(each value prints on its own line)
Task: Count or add up only the values that pass a test, and keep asking until the user types a good answer.
- Items 6 and 8: type the whole condition.
- Items 7 and 9: type the whole line. The 8 spaces in front are already there.
- When you click Run, the user's answers are typed for you. The comment in each program says what the user types.
Program A: Score: 85
Score: 62
Score: 70
Score: 91
Score: 45
Passed: 3
Program B: Total: 98
Program C: Pick 1 to 10: 0
Out of range.
Pick 1 to 10: 15
Out of range.
Pick 1 to 10: 7
You picked 7
Task: Each orange box has a bug. Some programs will not run, one never ends, and one prints the wrong numbers. Fix each box.
Program A: 1 odd 2 even 3 odd 4 even
Program B: 1 is odd 3 is odd 5 is odd
Program C: Play? yes
Playing round 1
Play? y
Playing round 2
Play? no
Rounds played: 2
Program D: -2 negative -1 negative 0 zero 1 positive 2 positive
Program E: 5 6 7 8 9 10
(in A, B, D, and E each value prints on its own line)
Task: Trace each loop on scratch paper. Each time through the loop, write down the variables and whether the
if is True or False. Check the loop condition one last time to see when it stops. Then choose the answer.TerminalProblemsOutput