Home > Article > Backend Development > Use Python to implement a simple guessing number game sample code
Python Implement a simple number guessing game, as follows:
Randomly generate a number between 1-10 for the user to guess. When the guess is wrong, the guesser will be prompted. Whether the number is larger or smaller until the user guesses correctly.
import random secret = random.randint(1,10) #print(secret) print('------猜数字游戏!-----') guess = 0 while guess != secret: temp = input('猜数字游戏开始,请输入数字:') guess = int(temp) if guess > secret: print('您输入的数字大了!') else: print('您输入的数字小了!') if guess == secret: print('恭喜,您猜对了!') print('游戏结束,再见!^_^')
The above is the detailed content of Use Python to implement a simple guessing number game sample code. For more information, please follow other related articles on the PHP Chinese website!