You are on page 1of 6

TOPIC 1: COMPUTATIONAL THINKING

Repetition (while)

ACTIVITIES SOLUTIONS FOR Y10-02-CT11

Activity 1
A program uses a count-controlled loop to count up to a maximum value. The output from
running this program is shown.

The code for the program is all jumbled up in the box below. Load the code into your
Integrated Development Environment (IDE) and arrange the lines to create a functional
program. Remember to use white space, comments, and indention to make your code
readable.

# ------------------------------------------------------------
# Constants
# ------------------------------------------------------------
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
count = count + 1
while (count <= MAX_VALUE):
MAX_VALUE = 6
print("Loop count: " + str(count) + " out of " + str(MAX_VALUE))
count = 1

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

Repetition (while)

Activity 2
A program uses a condition-controlled loop to repeat a process as long as the user wants to
keep going. The output from the program is shown.

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

Repetition (while)

The code for the program is all jumbled up in the box below. Load the code into your
Integrated Development Environment (IDE) and arrange the lines to create a functional
program. Remember to use white space, comments, and indention to make your code
readable.

# ------------------------------------------------------------
# Constants
# ------------------------------------------------------------
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
choice = choice.upper()
print("Mary had a little lamb ... ")
choice = ""
NO = "N"
choice = input("Would you like another (Y/N)? ")
YES = "Y"
print("Goodnight, Mary")
choice = input("Would you like another (Y/N)? ")
while (choice == YES):
print("... Lamb ...")
choice = choice.upper()

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

Repetition (while)

Activity 3
This algorithm allows a user to make a choice from a menu. The user chooses ‘Q’ to quit the
program. Arrange the symbols to create a flowchart for the algorithm. Use each symbol only
once. Use as many arrows as you need.

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

Repetition (while)

start

display menu

get user’s choice

Yes
is choice ==
display Goodbye
Q?

No
end

display choice

Activity 4
Copy and paste this code into your IDE.

userChoice = ""
while userChoice != "exit":
userChoice = input("Enter your choice: ")
print("You chose:", userChoice)
if userChoice == "hello":
print("Hi! Nice to meet you.")

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

Repetition (while)

Add more selection statements so that the program outputs the different values shown in the
table.

Input Output

hello Hi! Nice to meet you.

exit Goodbye.

joke Where does the Easter Bunny go when he


needs a new tail? To a re-tail store!

song Row, row, row your boat.

None of these Try again.

# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
userChoice = ""

# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
while userChoice != "exit":
userChoice = input("Enter your choice: ")
print("You chose:", userChoice)
if userChoice == "hello":
print("Hi! Nice to meet you.")
elif userChoice == "joke":
print("Where does the Easter Bunny go when he "
"needs a new tail? To a re-tail store!")
elif userChoice == "song":
print("Row, row, row your boat.")
elif userChoice == "exit":
print("Goodbye.")
else:
print("Try again")

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

You might also like