2016.8.21猜数游戏

 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)两种情况。

 

posted @ 2016-08-21 11:01  queqiaoshui  阅读(214)  评论(0)    收藏  举报