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

Python Next Steps Homework 2 Answers

Uploaded by

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

Python Next Steps Homework 2 Answers

Uploaded by

Shane Yap
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 [2]
total: 1
next: 2
(b) Complete the value of each variable after the last pass through the loop [2]
total: 7
next: 8

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: ”)
while choice != “rhino”:
print(“Incorrect. Try again.”)
choice = input(“Guess the animal: ”)
One mark for identifying that the question needs to be asked again
One mark for correctly indenting it inside the loop

1
Homework 2: Loops
Python Next Steps

4. State when a programmer should use a for loop instead of a while loop [1]
When the number of times to repeat is known

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(1,8):
papers = int(input(“How many papers delivered today? ”))
total = total + papers
print(“Total papers delivered =”, total)

total = 0
for counter in range(7): (accept range(0,7))
papers = int(input(“How many papers delivered today? ”))
total = total + papers
print(“Total papers delivered =”, total)

[Total 15 marks]

You might also like