python小游戏源代码
时间: 2023-06-23 07:59:04 浏览: 78
Python小游戏源码
5星 · 资源好评率100%
以下是一个简单的猜数字游戏的 Python 代码:
```python
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken += 1
if guess < number:
print('Your guess is too low.')
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number + '.')
```
这个游戏会生成一个 1 到 20 之间的随机整数,并让玩家猜测这个数字是多少。玩家每猜一次,程序都会告诉玩家猜的数字是太高了还是太低了。如果玩家在 6 次猜测内猜对了这个数字,程序会告诉玩家猜对了,并显示玩家猜了几次。如果玩家猜错了 6 次,程序会显示正确的数字。
阅读全文