我是靠谱客的博主 畅快老虎,这篇文章主要介绍用异常处理改编猜数游戏程序,现在分享给大家,希望可以做个参考。

用异常处理改编猜数游戏程序,功能是:允许用户反复输入数,直至猜中程序选定的数(假定为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。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

最后

以上就是畅快老虎最近收集整理的关于用异常处理改编猜数游戏程序的全部内容,更多相关用异常处理改编猜数游戏程序内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(83)

评论列表共有 0 条评论

立即
投稿
返回
顶部