Sometimes it is useful to use an infinite loop with a break statement to jump out of the loop. A good example would be a primitive version of a game menu. To exit a game menu the user has to type quit.
Example 1 Using the while loop and break statement create a simple main menu of the game.
Solution: This is a very primitive game menu that we will create in this example which consists of 5 different choices and these are 1. New Game, 2. Save Game, 3. Load Game, 4. Options, and 5. Quit. Then the user will have to provide an input i.e. type the choice from the game menu. If the user types "Quit" it will end the while loop using the break command. However, if the user types anything else it will show the game menu again.
Example 1 Using the while loop and break statement create a simple main menu of the game.
Solution: This is a very primitive game menu that we will create in this example which consists of 5 different choices and these are 1. New Game, 2. Save Game, 3. Load Game, 4. Options, and 5. Quit. Then the user will have to provide an input i.e. type the choice from the game menu. If the user types "Quit" it will end the while loop using the break command. However, if the user types anything else it will show the game menu again.
while True:The output of the previous code is given below.
print("""1. New Game\n2. Save Game\n3. Load Game\n4. Quit""")
USER_INPUT = input("Input:>")
if USER_INPUT == "Quit":
break
print("USER_INPUT = {}".format(USER_INPUT))
print("You have chosen to quit the game.")
1. New GameExample 2 Create a game using a while loop where the user has to guess the number that was randomly chosen with Python.
2. Save Game
3. Load Game
4. Quit
Input:>New Game
USER_INPUT = New Game
1. New Game
2. Save Game
3. Load Game
4. Quit
Input:>Save Game
USER_INPUT = Save Game
1. New Game
2. Save Game
3. Load Game
4. Quit
Input:>Load Game
USER_INPUT = Load Game
1. New Game
2. Save Game
3. Load Game
4. Quit
Input:>Quit
You have chosen to quit the game.
import randomOutput:
COMPUTER_NUMBER = random.randint(1,100000)
while True:
print("Guess the number that Python randomly chosen: ")
USER_INPUT = int(input("Enter a number:> "))
if USER_INPUT < COMPUTER_NUMBER:
print("The number is higher!")
elif USER_INPUT > COMPUTER_NUMBER:
print("The number is lower!")
if USER_INPUT == COMPUTER_NUMBER:
break
print("Congratulations!!! You have guessed the number!!!")
Enter a number:> 10000
The number is higher!
Guess the number that Python randomly chosen.
Enter a number:> 100000
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 50000
The number is higher!
Guess the number that Python randomly chosen.
Enter a number:> 90000
The number is higher!
Guess the number that Python randomly chosen.
Enter a number:> 95000
The number is higher!
Guess the number that Python randomly chosen.
Enter a number:> 98000
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 97500
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 97300
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 96000
The number is higher!
Guess the number that Python randomly chosen.
Enter a number:> 96500
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 96250
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 96200
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 96100
The number is higher!
Guess the number that Python randomly chosen.
Enter a number:> 96150
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 96125
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 96115
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 96105
The number is higher!
Guess the number that Python randomly chosen.
Enter a number:> 96109
The number is lower!
Guess the number that Python randomly chosen.
Enter a number:> 96107
Congratulations!!! You have guessed the number!!!