Guess the age game upgrade version

Guess the age game upgrade version

Require:

  1. Allow users to try up to 3 times
  2. After every 3 attempts, if the user has not guessed correctly, ask the user if he wants to continue playing. If the answer is Y or y, continue to let him guess 3 times, and so on. If the answer is N or n, exit the program
  3. How to guess right, just quit

1. The code is as follows:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author: davie
'''
Exercise 2: Age Guessing Game Upgrade (10 minutes)
Require:
Allow users to try up to 3 times
After every 3 attempts, if the user has not guessed correctly, ask the user if he wants to continue playing. If the answer is Y or y, continue to let him guess 3 times, and so on.
If you answer N or n, exit the program
How to guess right, just quit
'''
count = 0
age = 33
while count < 3:
    count += 1 
    age_input = int(input( " Please enter the age you guessed -->: " ))
     if age_input == age:
         print ( " Congratulations, you guessed correctly " )
         break 
    elif age_input > age:
         print ( " The age you guess is greater than the actual age, please guess smaller next time " )
     else :
         print ( " The age you guess is smaller than the actual age, please guess larger next time " )
     if count == 3 :
        choice = input( " Guess wrong 3 times in a row, whether to continue, y/n: " )
         if choice == " Y "  or choice == " y " :
            count = 0
         elif choice == " N "  or choice == " n " :
             break 
        else :
             print ( " Your input is wrong, the program ends " )

2. Test

D:\yc\pycharm\venv\Scripts\python.exe D:/yc/pycharm/python3/while_guess_age.py
Please enter your guessed age -->:12
The age you guess is smaller than the actual age, please guess bigger next time
Please enter your guessed age -->:22
The age you guess is smaller than the actual age, please guess bigger next time
Please enter your guessed age -->:32
The age you guess is smaller than the actual age, please guess bigger next time
Guess wrong 3 times in a row, whether to continue, y / n: y
Please enter your guessed age -->:22
The age you guess is smaller than the actual age, please guess bigger next time
Please enter your guessed age -->:33
Congratulations, you guessed it

Process finished with exit code 0
D:\yc\pycharm\venv\Scripts\python.exe D:/yc/pycharm/python3/while_guess_age.py
Please enter your guessed age -->:22
The age you guess is smaller than the actual age, please guess bigger next time
Please enter your guessed age -->:33
Congratulations, you guessed it

Process finished with exit code 0
D:\yc\pycharm\venv\Scripts\python.exe D:/yc/pycharm/python3/while_guess_age.py
Please enter your guessed age -->:44
The age you guess is greater than the actual age, please guess smaller next time
Please enter your guessed age -->:43
The age you guess is greater than the actual age, please guess smaller next time
Please enter your guessed age -->:42
The age you guess is greater than the actual age, please guess smaller next time
Guess wrong 3 times in a row, whether to continue, y / n: k
You made a mistake, the program ends

Process finished with exit code 0
D:\yc\pycharm\venv\Scripts\python.exe D:/yc/pycharm/python3/while_guess_age.py
Please enter your guessed age -->:44
The age you guess is greater than the actual age, please guess smaller next time
Please enter your guessed age -->:43
The age you guess is greater than the actual age, please guess smaller next time
Please enter your guessed age -->:42
The age you guess is greater than the actual age, please guess smaller next time
Guess wrong 3 times in a row, whether to continue, y / n: n

Process finished with exit code 0

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324457126&siteId=291194637