|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
就比如我第一次输入3,第二次输入3.14,想在第二次输出不规范的情况下,输出try-except语句使程序不报错。请各位大佬指点 !
import random
secret = random.randint(1,9)
times = 3
try:
temp = input('猜猜我心里想的是什么数字呀?\n')
guess = int(temp)
except (ValueError,EOFError,KeyboardInterrupt):
print('嘤嘤嘤,输入错误惹!')
guess = secret
while (guess != secret):
times -= 1
if guess == secret:
print('猜对咯,真棒!^_^ ')
else:
if guess > secret:
print('大了大了~~ ')
else:
print('小了小了~~ ')
if times > 0:
print('还有%d次机会,再猜一次吧~ '% times)
else:
print('机会用完了QAQ!,答案是%d,再玩一次吧~ '% secret)
break
temp = input()
guess = int(temp)
print('游戏结束啦!^_^ ')
本帖最后由 liuzhengyuan 于 2020-5-5 19:57 编辑
你的意思是想让程序认为小数是违规的对吧
那就放进 except:
在 excpet 里判断是否为小数
如果是小数,那就 guess = secret
|
|