0% found this document useful (0 votes)
5 views

Python Next Steps Homework 2 (1)

This document is a homework assignment focused on loops in Python, consisting of multiple questions related to loop functionality and code examination. It includes tasks such as identifying correct loop conditions, completing variable values after loop iterations, rewriting faulty code, and determining when to use for loops versus while loops. Additionally, it requires students to fill in missing code lines for a program calculating total papers delivered over a week.

Uploaded by

louis.mutzig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Next Steps Homework 2 (1)

This document is a homework assignment focused on loops in Python, consisting of multiple questions related to loop functionality and code examination. It includes tasks such as identifying correct loop conditions, completing variable values after loop iterations, rewriting faulty code, and determining when to use for loops versus while loops. Additionally, it requires students to fill in missing code lines for a program calculating total papers delivered over a week.

Uploaded by

louis.mutzig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Homework 2: Loops

Python Next Steps

Homework 2: Loops
1. A computer game allows a player to repeat a level until they run out of lives.
Which two of the following loops would work correctly? [2]
⬨ while lives < 0:
⬨ while lives > 0:
⬨ while lives == 0:
⬨ while lives != 0:

2. Examine the following code:


total = 0
next = 1
while total < 4:
total = total + next
next = next * 2

(a) Complete the value of each variable after the first pass through the loop

total: _______1____________
next: _________2__________
(b) Complete the value of each variable after the last pass through the loop [2]

total: _____7______________

next: ______8_____________

1
Homework 2: Loops
Python Next Steps

3. Examine the following code:


choice = input(“Guess the animal: ”)
while choice != “rhino”:
print(“Incorrect. Try again.”)
When the user tries to test this program with an incorrect answer it just repeats
“Incorrect. Try again” forever.
Rewrite the program below so that it works correctly. [2]
choice = input(“Guess the animal: ”)
if choice != “rhino”:
print(“Incorrect. Try again.”)

2
Homework 2: Loops
Python Next Steps

4. State when a programmer should use a for loop instead of a while loop [1]
When they want to control the amount of limes it loops

5. Examine the following code:


for counter in range(12):
print(counter)
State the first and the last values that will be printed. [2]
0,11

6. Examine the following code:


for counter in range(20,25):
print(counter)
State the first and the last values that will be printed [2]
20,24

7. A newsagent wants a program to add up the total number of papers delivered in a


period of one week (7 days).
Fill in the two incomplete lines of code. [2]
total = 0
for counter in range(0,8)
papers = int(input(“How many papers delivered today? ”))
total = papers + total
print(“Total papers delivered =”, total)

[Total 15 marks]

You might also like