在本文中,我们将演示如何创建一个简单的猜谜游戏。
游戏的目的是猜测正确的数字。
示例
下面运行示例:

使用 Python 的简单文字游戏
随机数
将要求用户猜测随机数。 我们首先选择随机数:
from random import randintx = randint(1,9)
randint()函数将选择一个介于 1 到 10 之间的伪随机数。然后,我们必须继续直到找到正确的数字为止:
guess = -1print("Guess the number below 10:")while guess != x:guess = int(raw_input("Guess: "))if guess != x:print("Wrong guess")else:print("Guessed correctly")
Python 猜测游戏
下面的代码开始游戏:
from random import randintx = randint(1,9)guess = -1print "Guess the number below 10:"while guess != x:guess = int(raw_input("Guess: "))if guess != x:print("Wrong guess")else:print("Guessed correctly")
运行示例:
Guess the number below 10:Guess: 3Wrong guessGuess: 6Wrong guess..
