Do a guessing game in Python (Introduction)
preface:
Hello, everyone. I'm sunset sampan. Because time is a little tight recently, I'll make a small program first. Thank you for your support. Due to your limited ability, you are welcome to correct the error.
My code is divided into two stages. The second stage is the perfect version of the first stage. Each stage has a complete code. I tried and didn't report an error. You can copy and test it.
First open the Python editor (see python.org download).
Phase I:
Start writing code. First, you need a random library (the number to guess should not even be known to the programmer).
import random #Or from random import random
Well, with the library, now name the number to guess. Of course, it's better to be an integer. Otherwise, who will play your game? The number to guess is called goal. The range is smaller, between 0 and 10.
goal = random.randint(0,10)
OK, set another player input variable num.
num = int(input("Please enter an integer from 0 to 10"))
By the way, the number entered by the player must be an integer. If it is not an integer or floating point number, an error will be reported. Now change the above code.
num = input("Enter an integer from 0 to 10:") try: num = int(num) except: print("Warning, please enter an integer:")
Well, now it's the most critical time. Now it's time to judge whether the number entered by the player is different from the random number one. How to judge? When the number (num) entered by the player is the same as the random number (goal), the "guess right" is output, and the code is as follows:
if num == goal : print("Guess right!!!")
When num is greater than goal, the output "guess big".
if num > goal: print("Guess big.")
When num is less than goal, output "guess small".
if num < goal: print("Guess it's small.")
OK, that's all for the first phase. The complete code:
import random #Or from random import random goal = random.randint(0,10) num = input("Enter an integer from 0 to 10:") #Attention ↓↓↓↓↓ try: num = int(num) except: print("Warning, please enter an integer:") #The exception handling here will have problems in the following judgment, and I will explain it in the second stage. if num == goal : print("Guess right!!!") if num > goal: print("Guess big.") if num < goal: print("Guess it's small.")
Phase II:
Now there is a problem to be solved first. When num is not a number (integer or decimal), the editor will report an error while handling the exception, as shown in the figure:
Enter an integer from 0 to 10:q Warning, please enter an integer: Traceback (most recent call last): File "C:/Users/Administrator/Python/Python38-32/Guessing game.py", line 14, in <module> if num > goal: TypeError: '>' not supported between instances of 'str' and 'int'
The original code is directly executed to the judgment, and the player has not re entered it, so the judgment should be changed to exception handling.
try: num = int(num) if num == goal : print("Guess right!!!") if num > goal: print("Guess big.") if num < goal: print("Guess it's small.") except: print("Warning, please enter an integer:")
Now there is no error, but the player still cannot re-enter. First change the print to input, and then judge.
try: · · · · · · except: num = input("Warning, please enter an integer:") #The next steps are the same as above num = int(num) if num == goal : print("Guess right!!!") if num > goal: print("Guess big.") if num < goal: print("Guess it's small.")
However, players can only guess once. We changed it to 5 times, but first the code is too difficult to write, so first load the judgment code into the choose function.
Note: the variables in the function are global variables, so the global variables should be declared before num and goal.
import random global num,goal goal = random.randint(0,10) num = input("Enter an integer from 0 to 10:") try: num = int(num) if num == goal : print("Guess right!!!") if num > goal: print("Guess big.") if num < goal: print("Guess it's small.") except: num = input("Warning, please enter an integer:") num = int(num) if num == goal : print("Guess right!!!") if num > goal: print("Guess big.") if num < goal: print("Guess it's small.")
After declaring the global variable, now declare the function.
import random global num,goal goal = random.randint(0,10) num = input("Enter an integer from 0 to 10:") #This is the function written def choose(): num = int(num) if num == goal : print("Guess right!!!") if num > goal: print("Guess big.") if num < goal: print("Guess it's small.") # try: choose() except: num = input("Warning, please enter an integer:") choose()