大家帮我看看这么改进小游戏有什么问题吗
print('0425python练习')import random
secret = random.randint(1,9)
times = 3
temp = input('你有3次机会请从1到9之间猜测一个数字:')
guess = int(temp)
if guess == secret:
print('你好聪明猜对了~')
else:
while (guess!=secret) and (times>0):
times = times-1
if guess>secret:
temp = input('猜大了请重新猜:')
guess = int(temp)
else:
temp = input('猜小了请重新猜:')
guess = int(temp)
if guess == secret:
print('猜对了,恭喜你!')
print('game over~~')
没问题啊 摸得问题 没问题啊……没必要把你一点问题的代码发上来让我们看,自己运行一下不就知道对错了 两个问题:
1.times应该是2,因为第一次猜也算用了一次机会
2.不管怎样,都会打印game over~(猜对了也over)所以加了一个判断语句
正确代码:
print('0425python练习')
import random
secret = random.randint(1,9)
times = 2
temp = input('你有3次机会请从1到9之间猜测一个数字:')
guess = int(temp)
if guess == secret:
print('你好聪明猜对了~')
else:
while (guess!=secret) and (times>0):
times = times-1
if guess>secret:
temp = input('猜大了请重新猜:')
guess = int(temp)
else:
temp = input('猜小了请重新猜:')
guess = int(temp)
if guess == secret:
print('猜对了,恭喜你!')
if times == 0:
print('game over~~')
页:
[1]