python小游戏完整代码
时间: 2023-07-23 08:35:17 浏览: 65
以下是一个简单的猜数字游戏的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 = 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次之内猜中了,程序会输出“Good job, [用户名]! You guessed my number in [猜测次数] guesses!”,否则程序会输出“Nope. The number I was thinking of was [正确数字].”
阅读全文