从零开始的Linux运维屌丝之路,资源免费分享平台   运维人员首选:简单、易用、高效、安全、稳定、社区活跃的开源软件

7、Python、While循环猜年龄作业

发布:蔺要红04-04分类: Python

Python快速入门:while循环猜年龄作业
 
1、猜年龄游戏,准许用户最多猜3次、中间猜对了直接退出循环。
2、猜年龄游戏,准许用户最多猜3次、中间猜对了直接退出循环。当3次以后还没猜对,询问用户是否还想再玩、
     如果选择Y则准许用户再猜三次、如果选择N则直接退出循环


第一题解答:
[root@test01 /home]# cat guess_age03.py 
#!/usr/bin/python
#Author:linyaohong
count = 1
age = 26
while count <=3:  
    user_guess = int(input("You guess:"))
    if user_guess == 26:
        print("You True")
        break
    elif user_guess < age:
        print("try bigger")
    else:
        print("try smaller")
    count +=1
    
[root@test01 /home]# python guess_age03.py 
You guess:26
You True
[root@test01 /home]# python guess_age03.py 
You guess:12
try bigger
You guess:14
try bigger
You guess:15
try bigger
[root@test01 /home]# python guess_age03.py 
You guess:12
try bigger
You guess:26
You True
[root@test01 /home]# python guess_age03.py 
You guess:33
try smaller
You guess:26
You True
 第二题解答:
[root@test01 /home]# cat guess_age04.py 
#!/usr/bin/python
#Author:linyaohong
count = 1
age = 26
while count <=3:  
    user_guess = int(input("You guess:"))
    if user_guess == 26:
        print("You True")
        break
    elif user_guess < age:
        print("try bigger")
    else:
        print("try smaller")
    count +=1
    if count == 4:
        choice = input("Do You want go?(y|Y)")
        if choice == "y" or choice == "Y":
            count = 0
[root@test01 /home]# python guess_age04.py 
You guess:22
try bigger
You guess:55
try smaller
You guess:11
try bigger
Do You want go?(y|Y)y
You guess:55
try smaller
You guess:26
You True
[root@test01 /home]# python guess_age04.py 
You guess:44
try smaller
You guess:556
try smaller
You guess:1
try bigger
Do You want go?(y|Y)n
 
温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,如有侵权我会在24小时之内删除!

欢迎使用手机扫描访问本站