You are on page 1of 5

TOPIC 1: COMPUTATIONAL THINKING

Repetition (while)

HOMEWORK SOLUTIONS FOR Y10-02-CT11

Question 1
In this program, the user enters a number. A condition-controlled loop prints a counter
statement each time the program runs through the loop. The program also sums the
numbers from 1 to the number entered.
Here is the console output from the program.

The lines in the program are jumbled up in the box below. Arrange the lines of code to create
a functional program. You will need to do this in your Integrated Development Environment
(IDE).

# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
count = int(input ("Enter a number of times to loop (1-5)"))
startCount = 0
count = 0
total = 0
print("Loop count: " + str(count))
total = total + count
while (count > 0):
startCount = count
count = count - 1
print("Sum of numbers " + str(startCount) + " to 1 is: " +
str(total))

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
1
TOPIC 1: COMPUTATIONAL THINKING

Repetition (while)

# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
startCount = 0
count = 0
total = 0

# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------

count = int (input ("Enter a number of times to loop (1-5)"))


startCount = count
while (count > 0):
print ("Loop count: " + str(count))
total = total + count
count = count - 1
print ("Sum of numbers " + str(startCount) + " to 1 is: " +
str(total))

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
2
TOPIC 1: COMPUTATIONAL THINKING

Repetition (while)

Question 2
Write a program that allows the user to choose an ice cream flavour from a list of flavours.
The user can choose (V)anilla, (C)hocolate, or (S)trawberry flavours or choose to (E)xit the
program. If the user chooses any other flavour, the program should display a message
showing that that flavour is ‘out of stock’. The program should keep going in a loop until the
user chooses (E)xit. It should then print the total number of ice creams selected for each
flavour.

# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
flavour = "" # No flavour to start with
numVanilla = 0
numChocolate = 0
numStrawberry = 0

# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------

while (flavour != "E"):


flavour = input("(V)anilla, (C)hocolate, (S)trawberry, (E)xit: ")
flavour = flavour.upper()
if (flavour == "V"):
numVanilla = numVanilla + 1
elif (flavour == "C"):
numChocolate = numChocolate + 1
elif (flavour == "S"):
numStrawberry = numStrawberry + 1
elif (flavour != "E"):
print("Not in stock")

print("Vanilla: " + str(numVanilla) + " " +


"Chocolate: " + str(numChocolate) + " " +
"Strawberry: " + str(numStrawberry))

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
3
TOPIC 1: COMPUTATIONAL THINKING

Repetition (while)

Question 3
The modulus function (%) can be used to check if an integer is even. If a number MODULUS
2 equals 0, then the number is even, otherwise, the number is odd.
Here is the code for a program that identifies if a number is even or odd.

# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
number = 0

# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
number = int (input ("Enter a number between 1 and 100. 0 to exit: "))
while(number != 0):
if (number % 2 == 0):
print(str(number) + " is even")
else:
print(str(number) + " is odd")
number = int(input ("Enter a number between 1 and 100. 0 to exit: "))

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
4
TOPIC 1: COMPUTATIONAL THINKING

Repetition (while)

Draw a flowchart that shows how this algorithm works. Remember, you will need a decision
symbol for the ‘while’ loop and a decision symbol for the ‘if/else’ statement.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
5

You might also like