概述
用异常处理改编猜数游戏程序,功能是:允许用户反复输入数,直至猜中程序选定的数(假定为100)。输入的数如果大于选定的数,则提示"larger than expected";如果小于选定的数,则提示"less than expected";如果用户输入的不是整数,则提示"input error";如果等于选定的数,则输出"you have tried N times, you win"并结束程序。
【输入形式】
一次或多次输入整数
【输出形式】
对于每一次输入,新起一行输出对于猜数结果的提示。
【样例输入】
50
150
E
100
【样例输出】
less than expected
larger than expected
input error
you have tried 4 times, you win
【说明】
被猜的数设定为100。
a=100
b=1
count = 0
while True:
try:
count += 1
d=int(input())
except ValueError:
print("input error")
else:
if a==d:
print("you have tried %d times, you win"%(count))
break
elif a<d:
print("larger than expected")
else:
print("less than expected")
b+=1
a=100
b=1
count = 0
while True:
try:
#count += 1
d=int(input())
count +=1#错误写法
except ValueError:
print("input error")
else:
if a==d:
print("you have tried %d times, you win"%(count))
break
elif a<d:
print("larger than expected")
else:
print("less than expected")
b+=1
最后
以上就是畅快老虎为你收集整理的用异常处理改编猜数游戏程序的全部内容,希望文章能够帮你解决用异常处理改编猜数游戏程序所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复