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
Counting loop: start, condition, update
count = 1 # 1. start
while count <= 5: # 2. condition
print(count)
count += 1 # 3. update
Accumulator: a running total
total = 0 # start at 0
num = 1
while num <= 4:
total += num # add to the total
num += 1
print(total) # 10
Keep asking until it is right
guess = input("Password: ")
while guess != "python":
print("Try again.")
guess = input("Password: ")
print("Welcome!")
- A
whileloop repeats its indented body as long as the condition is True. After the body runs, Python goes back up and checks the condition again. - Create the loop variable before the loop, and update it inside the body (indented 4 spaces) so the condition eventually becomes False.
<stops before the number,<=includes it. Always check the first and last values printed.- If the condition is False at the start, the loop runs zero times. That is not an error.
passis an instruction that does nothing. It holds the spot in an empty body. A comment alone is not enough.
Messages you may see in the terminal
Stopped: this loop ran 1000 times...means an infinite loop: the condition never becomes False. Check that there is an update, that it is indented inside the loop, and that it moves the right way. (Real Python keeps going until you press Ctrl + C, which showsKeyboardInterrupt.)IndentationError: expected an indented block after 'while' statementmeans the body is not indented, or it is only a comment. Usepassfor an empty body.SyntaxError: expected ':'means thewhileline is missing its colon.- No error at all is possible too! Off-by-one mistakes and loops that run zero times give no message. 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_Loops_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 | Counting loops: start, condition, update, and counting down | 1 to 5 | 5 |
| 2 | Accumulators and loops with input() | 6 to 10 | 5 |
| 3 | Debug: infinite loops, off-by-one, a loop that never runs, and pass | 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: Complete each counting loop so it prints what its comment says.
- Item 3: type the whole update line. The 4 spaces in front are already there.
- Item 5: type the whole condition.
Program A: 1 2 3 4 5
Program B: 2 4 6 8 10
Program C: 5 4 3 2 1 Liftoff!
(each value prints on its own line)
Task: Build a running total, and loops that keep asking until the answer is right.
- Items 7, 9, and 10: type the whole line. The 4 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: Total: 55
Program B: Password: java
Try again.
Password: Python
Try again.
Password: python
Welcome!
Program C: Guess: 3
Guess: 9
Guess: 7
Got it in 3 tries
Task: Each orange box has a bug. Some programs never end, some stop too early or never start, and one will not run at all. Fix each box.
Program A: 3 2 1
Program B: 1 2 3 Done
Program C: 1 2 3 4 5 6 7 8 9 10
Program D: 1 2 3 4
Program E: Report done.
(each value prints on its own line)
Task: Trace each loop on scratch paper. Write down the loop variable after every time through the loop, and check the condition one last time to see when it stops. Then choose the answer.
TerminalProblemsOutput