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

python循环之猜年龄游戏

时间:2018-07-26 21:10:20      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:break   big   输入   年龄   lse   升级   ant   print   游戏   

一、使用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

 

python循环之猜年龄游戏

标签:break   big   输入   年龄   lse   升级   ant   print   游戏   

原文地址:https://www.cnblogs.com/SongjiangCyclone/p/9373918.html

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