In this blog, we write a game of guessing numbers. First of all, let's take a look at our needs, that is, what this game needs us to do.
1. Give a range of numbers from 1 to 250 and randomly generate a number as the answer. The player guesses a number between 1 and 250 and enters it into the program. If he guesses correctly, it means that he has won the game. If he guesses wrong, he will be prompted to be big or small. In this way, he will circle seven times. How many times does each guess prompt still exist? After seven guesses, he will prompt the game to fail. .
2. Deal with the error that the user input is not an integer, and prompt to enter an integer.
With the above requirements, let's start coding. First, create a. py document.
Write a framework first
from random import randint # Calling random library to generate random numbers ans = randint(1,250) # Get a ready-to-use library as the answer gus = int(input("Enter an integer of 1 to 250:")) # Gets a number entered by the user and converts it from a string to a number # If the number is right and the result is right, the wrong prompt is bigger or smaller. if gus == ans: print("Yes, that's right.") elif gus > ans: print("It's getting bigger.") elif gus < ans: print("It's small.")
This realizes the situation of guessing once. If you guess seven times, you can use a loop.
As follows:
# encoding:utf-8 from random import randint # Calling random library to generate random numbers ans = randint(1,250) # Get a ready-to-use library as the answer i = 0 # Used for recording times while True: gus = int(input("Enter an integer of 1 to 250:")) # Gets a number entered by the user and converts it from a string to a number i += 1 if gus == ans: print("Yes, that's right.") break #If you guess right, you end the program. elif gus < ans: print("It's small.") print("You still have{}Second chance".format(7-i)) elif gus > ans: print("It's getting bigger.") print("You still have{}Second chance".format(7-i)) if i == 7: print("You have no chance") break # The number of times is up, the game is over.
That's the full code for guessing digital games. We can write out the simplicity first and then modify the program as required. The following trial run results:
But there is another problem. If I enter a random mess, I will make a mistake.
So here's another exception handling:
# encoding:utf-8 from random import randint # Calling random library to generate random numbers ans = randint(1,250) # Get a ready-to-use library as the answer i = 0 # Used for recording times while True: try: gus = int(input("Enter an integer of 1 to 250:")) # Gets a number entered by the user and converts it from a string to a number except: print("Your input is incorrect, please enter an integer!") continue #If an exception occurs, the correct input is prompted and the current loop is terminated. i += 1 if gus == ans: print("Yes, that's right.") break #If you guess right, you end the program. elif gus < ans: print("It's small.") print("You still have{}Second chance".format(7-i)) elif gus > ans: print("It's getting bigger.") print("You still have{}Second chance".format(7-i)) if i == 7: print("You have no chance") break # The number of times is up, the game is over.
The operation is as follows:
That's all for guessing the number game.