Skip to content
Learnearn.uk » Python Unit Home » Continuous Repetition with While Loops

Continuous Repetition with While Loops

Beginner

Introduction to While Loops

YouTube blocked? Use the Google Drive version

Advanced

Cheat Sheet

Cheat Sheet

#Example 1
x = 0 
while x < 5:
    print("goodbye")
    x += 1

#Example 2
name = ""
while name.lower() != "mr west":
    name = input("Who is the best teacher?")

#Example 3
total = 0
while total <= 20:
    amount = int(input("How much to add?"))
    total = total + amount

#Example 4      
tries_left = 5
correct = False
while tries_left > 0 and correct == False:
    answer = input("What is 5 times 5?")
    if answer == "25":
        correct = True
    else:
        tries_left = tries_left - 1

if correct:
    print("Well done!")
else:
    print("Loser!")

#Example 5
while True:
    num = input("Please enter a number")
    if not num.isdigit():
        break

    elif num == 0:
        continue
    else:
        num = int(num)
        print("The square of the num is:", num * num)

Printable Version

Challenge 13

Challenge 13 – Login Screen App

YouTube blocked at school? Use the Google Drive Version 

Login Screen App

Bronze

  • Create an app that repeatedly asks the user for their password until the correct password is entered.

Silver

  • Adapt your program so that it asks for both a username and password.
  • Use os.system(‘clear’) and time.sleep(1) to improve your app.

 

Gold

  • Limit the number of login attempts using a variable to keep track

14

Challenge 14 – Joke App

Everybody loves jokes! Create an app that asks users if they want to hear a joke.
If they answer yes tell them a joke.
Keep repeating the question until they answer yes.

YouTube blocked at school? Use the Google Drive Version.

Bronze

  • Ask the yes/no question, when they answer yes break out of the loop and tell the joke.

Silver

  • If they answer no, provide suitable feedback and ask the question again.
  • Use the .lower()  method to accept lower or uppercase input.

Gold

  • Clear the screen and use time.sleep() where appropriate make your app awesome.
  • Add some ASCII Art to your joke app.

15

Challenge 15 – Darts 501 to zero Score calculator

In darts players start at 501 and work their way down to zero. Create an app that keeps track of the player’s score and tells them when they have reached zero.

 Bronze

  • The app should create a score variable, set it to 501 and then keep asking the user to enter their score until they get down to zero.

 Silver

  • The program should tell the user how many throws they took.
  • The program should only accept number inputs and only accept inputs between 0 and 180.

 Gold

  • Create a two player version of the game and let the players know who has won.

16

Challenge 16 – Random number guesser game

You should write a program that asks the user to guess a random number.

Bronze

  • Generate a random number between 0 and 100 and ask the user to guess the number.
  • Keep asking the user to guess until they correctly guess the hidden number.

Silver

Give the user a hint each time to tell them if they are too high or too low.

Gold

  • Keep track of how many guesses the user made and tell them at the end.
  • Add the option to select a difficulty level and give them a different number of guesses depending on the option selected.
    • Easy – unlimited tries
    • Medium – 10 tries
    • Hard – 5 tries.

ASCII Art

ASCII Art

How to display ASCII Art in your programs