Lesson 8: While Loop Exercises
Now that you know what a while loop does and you've got the basics down, I want you to create your first programs without any help. It will be an exercise to practice your programming skills.
1st Exercise:
The program asks the user to input a number smaller than 5. If they don't input it, the program will keep asking until the user inputs the correct number. Afterwards, it prints how many attempts it took to enter it.
Reveal Answer
number = int(input("Write a number smaller than 5: "))
count = 1
while number >= 5:
print("The number is not smaller than 5!")
number = int(input("Try again: "))
count +=1
print(f"You did it! It took you {count} tries")Output
Write a number smaller than 5: 6
The number is not smaller than 5!
Try again: 5
The number is not smaller than 5!
Try again: 99
The number is not smaller than 5!
Try again: 2
You did it! It took you 4 tries2nd Exercise:
(This one is a little harder; you need to use the len() function)
From a text inputted via the keyboard, count how many vowels are in it.
Reveal Answer
text = str(input("Enter text: "))
vowels = "aeiouAEIOU"
index = 0
count = 0
while index < len(text):
if text[index] in vowels:
count += 1
index += 1
print("In the text there are ", count, " vowels")Output
Enter text: Hello World!
In the text there are 3 vowels3rd Exercise (The Hardest):
Game Twenty-One:
- You start with 0 points.
- The program in every round prints how many points you have and asks you if you want to continue.
- If you answer "no", the game ends.
- If you answer "yes", the program "turns a card" (randomly chooses a number from 2 to 10), prints its value, and adds it to your points.
- If you have more than 21 points, you lose.
- The goal is to gain as many points as you can. The perfect score is 21 points.
In this game, you will need to use the random library. All you need to do is write import random at the start of the program, and then create a variable that saves a random number from some range using the command random.randint(x, y), where x is the starting number and y is the ending number.
Reveal Answer
import random
points = 0
while points < 21:
print("You currently have ", points, "points")
a = (input("Do you want to continue? (yes/no): "))
if a == "yes":
card = random.randint(2,10)
print(f"Your number is {card}")
points += card
if a == "no":
break
if points == 21:
print("You got the perfect score 21!")
elif points > 21:
print(f"You lose! your points are {points}")
elif points < 21:
print(f"You got {points} points")Output
You currently have 0 points
Do you want to continue? (yes/no): yes
Your number is 5
You currently have 5 points
Do you want to continue? (yes/no): yes
Your number is 10
You currently have 15 points
Do you want to continue? (yes/no): yes
Your number is 3
You currently have 18 points
Do you want to continue? (yes/no): yes
Your number is 6
You lose! your points are 24Of course, in all of these exercises, you could have done it a different way than me, and that is absolutely okay. I put my codes here just in case you get lost and want to see how to do it.
Want XP, levels and progress tracking? Play this lesson in the CodeStart game.
Play Lesson 8