码迷,mamicode.com
首页 > 编程语言 > 详细

用Python写一个猜数字游戏

时间:2015-05-26 09:08:06      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

2015.5.25第一天下载Python IDLE,写个猜数字的小游戏来熟悉这门语言:

 1 times=6
 2 letters=[100]
 3 for i in range(1,times):
 4     a = input("input the number you guess:")
 5     try:
 6         b = int(a)
 7         if isinstance(b,int):
 8             if i <5:
 9                 if int(a) in letters:
10                     print("%s is the right answer, you win!" % a)
11                     break
12                 else:
13                     sen1 = "%s is not the right answer," % a
14                     if int(a) < letters[0]:
15                         sen2 = " and it is smaller,please try again."
16                     else:
17                         sen2 = " and it is greater, please try again."
18                     sen = "%s%s" % (sen1,sen2)
19                     print("%s" % sen)
20                     continue
21             else:
22                 print("You lose!")
23         else:
24             print("You should input an integer number")
25     except Exception as err:
26         print("You should input an integer number.")
27 input("Input any key to quit:")

 只有4次机会,都猜错了就算输,包括对数据输入的类型控制,就这样:

技术分享

用Python写一个猜数字游戏

标签:

原文地址:http://www.cnblogs.com/LanTianYou/p/4529627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!