while/for循环+if猜年龄

初学python,贴一段代码!
1. 先来个猜年龄的玩法

 1 _oldboy_age = 56
 2 count = 0
 3 while count <3:
 4     guess_age = int(input("guess_age:"))
 5     if guess_age == _oldboy_age:
 6        print("yes ,you right!")
 7        break
 8     elif guess_age > _oldboy_age:
 9           print("think smaller")
10     else:
11            print("think bigger")
12 
13     count +=1
14     if count == 3:
15         confirm = input("Do want to keep guessing?")
16         if confirm != 'n':
17             count = 0
18 
19 
20 else:
21 
22     print("you have try too much times,get off!")

 

 1 _oldboy_age = 56
 2 
 3 for a in range(3):
 4     guess_age = int(input("guess_age:"))
 5     if guess_age == _oldboy_age:
 6        print("yes ,you right!")
 7        break
 8     elif guess_age > _oldboy_age:
 9           print("think smaller")
10     else:
11         print("think bigger")
12 
13 else:
14 
15     print("you have try too much times,get off!")

 

posted @ 2017-10-08 17:48  子夜星云  阅读(184)  评论(0编辑  收藏  举报