码迷,mamicode.com
首页 > 其他好文 > 详细

2016.8.21猜数游戏

时间:2016-08-21 12:29:58      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

 1 import random
 2 secret=random.randint(1,99)
 3 guess=0
 4 tries=0
 5 print secret
 6 print"Alex,I have a secret,It is a number from 1 to 99,you have 6 tries."
 7 while guess!=secret and tries<6:
 8     guess=input("Enter you guess number:")
 9     if guess<secret:
10         print " too low"
11     elif guess>secret:
12         print"too hight"
13     tries+=1
14 if guess==secret:
15     print"congratulations"
16 else:
17     print "no more tries"

1.关于tries+=1,在python中,语句块不会单独引进新的作用域,tries还是全局变量,while中改变tries会使tries变量的值改变。

2.逻辑判断。(1)guess!=secret并且tries<6.(2)guess==secret,(3)tries>=6。当(1)不满足时,有(2)(3)两种情况。

 

2016.8.21猜数游戏

标签:

原文地址:http://www.cnblogs.com/queqiaoshui/p/5792318.html

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