Practical Challenges 5

<< Previous: While loopsNext: Lists >>

About the challenges

The challenges below allow you to apply your knowledge of the topics up to this point. Attempt the challenges in a Python IDE of your choice.

Challenge 1: Get it right

Write a program that asks the user a question and keeps repeating the question until they get it right.





sky = input("What colour is the sky? ")
while sky != "blue":
    print("That was wrong.")
    sky = input("What colour is the sky? ")
print("Well done, you guessed it.")

Challenge 2: Average of any number of numbers





total = 0
count = 0
num = 0
while num != -1:
    num = int(input("Enter a number or enter -1 to average the numbers entered so far"))
    count = count + 1
    total = total + num
average = total / count
print(f"The average is {average}")

Challenge 3: Validating input





choice = 0
while choice < 1 or choice > 4:
    choice = int(input("Enter a choice from 1 to 4: "))
print("You chose {choice}.")
<< Previous: While loopsNext: Lists >>

© All materials created by and copyright S.Goff