|
发表于 2020-11-30 14:33:12
|
显示全部楼层
本楼为最佳答案
输入合法性判断,正如前面鱼油回答,input返回的是一个字符串,所以会进入死循环,
程序还有一个问题,我觉得if 语句还可以改,
if times >= 2:
print('还有%d次机会'%(times),end='')
temp = input('请输入一个整数')
这样当我们改变次数times 的时候,程序运行才合理。
import random
times =10
secret = random.randint(1,10)
guess = 0
print('我们来玩个游戏吧')
temp =int(input('请输入我心里想的一个数字:'))
while type(temp) != type(1):
print('抱歉,输入不合法,', end='')
temp = input('请输入一个整数:')
while (guess != secret) and (times > 0):
guess = int(temp)
times= times - 1
if guess == secret:
print('哇,被你猜中了!')
print('但是猜中了也没有奖励~')
break
else:
if guess > secret:
print('大了大了')
if times >= 2:
print('还有%d次机会'%(times),end='')
temp = input('请输入一个整数')
else:
if times == 1:
print('还有1次机会',end='')
temp = input('请输入一个整数:')
else:
print('没机会啦')
else:
print('小了小了')
if times >= 2:
print('还有%d次机会'%(times),end='')
temp = input('请输入一个整数')
else:
if times == 1:
print('还有1次机会',end='')
temp = input('请输入一个整数')
else:
print('没机会啦')
print('游戏结束') |
|