python循环之猜年龄游戏

一、使用while循环的猜年龄游戏:

 1 # Author:yebo
 2 count = 0
 3 age_of_yebo = 20
 4 while count < 3:
 5 
 6     guess_age = int(input("guess age:"))    #input默认输入的数字是字符串形式,要转成int型
 7 
 8     if age_of_yebo == guess_age:
 9         print("Congratulations!")
10         break
11     elif age_of_yebo > guess_age:
12         print("guess bigger!")
13     else:
14         print("guess smaller!")
15     count +=1
16 else:
17     print("you tried too many times.")

二、使用for循环的猜年龄游戏:

 1 # Author:yebo
 2 
 3 age_of_yebo = 20
 4 
 5 for i in range(3):
 6     guess_age = int(input("guess age:"))   #input默认输入的数字是字符串形式,要转成int型
 7 
 8     if age_of_yebo == guess_age:
 9         print("Congratulations!")
10         break
11     elif age_of_yebo > guess_age:
12         print("guess bigger!")
13     else:
14         print("guess smaller!")
15 else:
16     print("you tried too many times.")

三、升级版猜年龄游戏:

 1 # Author:yebo
 2 count = 0
 3 age_of_yebo = 20
 4 while count < 3:
 5 
 6     guess_age = int(input("guess age:"))
 7 
 8     if age_of_yebo == guess_age:
 9         print("Congratulations!")
10         break
11     elif age_of_yebo > guess_age:
12         print("guess bigger!")
13     else:
14         print("guess smaller!")
15     count +=1
16     if count == 3:
17         continue_confirm = input("do you want to keep trying?")
18         if continue_confirm != "n":
19             count = 0

猜你喜欢

转载自www.cnblogs.com/SongjiangCyclone/p/9373918.html