python循环之猜年龄游戏

Posted songjiangcyclone

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于python循环之猜年龄游戏的主要内容,如果未能解决你的问题,请参考以下文章

Python基础之猜数游戏

python基础之猜数字游戏

java之猜数字游戏

python3之猜数字游戏

Python 基础实战 -- 小游戏之猜数字

python之猜数字