WK19 - Python - Loop - Answer
WK19 - Python - Loop - Answer
Exercise 1: Make a list of your favorite subjects in school. Use a for loop to print each
subject on a new line.
Exercise 2: Use a for loop and range() to print the numbers from 5 to 15.
Exercise 3: Ask the user to enter their name. Then, use a for loop to print "Hello" and their
name 3 times.
Exercise 1: Write a while loop that prints the numbers from 1 to 10.
number = 1
while number <= 10:
print(number)
number = number + 1
Exercise 2: Write a while loop that asks the user to guess a secret number (you can choose
the number). The loop should keep asking until the user guesses correctly. (For an extra
challenge, give them a hint like "Too high" or "Too low".)
secret_number = 7
guess = int(input("Guess the secret number (between 1 and 10): "))
while guess != secret_number:
if guess < secret_number:
print("Too low!")
else:
print("Too high!")
guess = int(input("Guess again: "))
print("You guessed it!")
Exercise 3: Write a while loop that simulates a simple countdown timer. The user enters a
starting number, and the loop counts down to 0.