# Python 和简单文字游戏 > 原文: [https://pythonspot.com/simple-text-game](https://pythonspot.com/simple-text-game) 在本文中,我们将演示如何创建一个简单的猜谜游戏。 游戏的目的是猜测正确的数字。 ## 示例 下面运行示例: ![python-text-game](img/e700a43a387c917fef1acdb35ac275a0.jpg) 使用 Python 的简单文字游戏 ## 随机数 将要求用户猜测随机数。 我们首先选择随机数: ```py from random import randint x = randint(1,9) ``` `randint()`函数将选择一个介于 1 到 10 之间的伪随机数。然后,我们必须继续直到找到正确的数字为止: ```py guess = -1 print("Guess the number below 10:") while guess != x: guess = int(raw_input("Guess: ")) if guess != x: print("Wrong guess") else: print("Guessed correctly") ``` ## Python 猜测游戏 下面的代码开始游戏: ```py from random import randint x = randint(1,9) guess = -1 print "Guess the number below 10:" while guess != x: guess = int(raw_input("Guess: ")) if guess != x: print("Wrong guess") else: print("Guessed correctly") ``` 运行示例: ```py Guess the number below 10: Guess: 3 Wrong guess Guess: 6 Wrong guess .. ```