This article is aimed only at novice, I hope you do not spray, thank you! If you don’t say much, you should first code.
import random if __name__ == '__main__': yourname = input("Hello! What's your name? \n"); print ("Welcome to guessing game." + yourname) print ("I guess a number is between 1 and 20. Can you guess it?") random_num = random.randint(1,20) time = 0 while time < 5: num = int(input("Please input your number:")) if num == random_num: break; elif num < random_num: print ("Smaller than mine.") else: print ("It's bigger than mine.") time = time+1 if time < 5: print ("Congratulations, you win.") else: print ("Don't lose heart. Once again, you can do it.")
Now let's sort out the programming ideas:
For if __name__ = ='__main__': it can be understood as a program entry. Please refer to Baidu for details.
In order to make the interface more friendly, you can print some things that you like.
First, call the random module to generate a specified number automatically.
Then compare the input numbers with the generated numbers, and compare them in the while True loop first.
while True: num = int(input("Please input your number:")) if num == random_num: break; elif num < random_num: print ("Smaller than mine.") else: print ("It's bigger than mine.")
Finally, the cyclic condition is replaced by the number of times, the initial value is assigned to 0, and the number time + 1 is entered once. If the number exceeds, the game will fail. If the number does not exceed the successful match, the game will break out.
At this point, a simple digital game is finished, the novice is on the road, thanks for your advice.
