0% found this document useful (0 votes)
96 views5 pages

Y10 02 CT11 Homework Solutions

This document contains homework solutions for a computational thinking course involving repetition using while loops. It includes the code for 3 programs: 1) a program that uses a while loop to print a counter and calculate a running sum, 2) a program that uses a while loop to allow a user to select ice cream flavors until exiting, tracking the total of each flavor, and 3) a program that uses a while loop and if/else statement to check if a user-input number is even or odd. A flowchart is also provided showing how the even/odd number checking algorithm works with decision symbols for the while loop and if/else statement.

Uploaded by

18gmills
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views5 pages

Y10 02 CT11 Homework Solutions

This document contains homework solutions for a computational thinking course involving repetition using while loops. It includes the code for 3 programs: 1) a program that uses a while loop to print a counter and calculate a running sum, 2) a program that uses a while loop to allow a user to select ice cream flavors until exiting, tracking the total of each flavor, and 3) a program that uses a while loop and if/else statement to check if a user-input number is even or odd. A flowchart is also provided showing how the even/odd number checking algorithm works with decision symbols for the while loop and if/else statement.

Uploaded by

18gmills
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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