代码改变世界

python学习猜年龄游戏和进阶

2017-12-09 11:00  龙武大帝  阅读(301)  评论(0编辑  收藏  举报

1、python学习猜年龄游戏if...elif...else

age_of_chuck = 25

_age_of_chuck = int(input("请猜chuck的年龄: "))

if _age_of_chuck == age_of_chuck:
    print("恭喜你猜对了!")
elif _age_of_chuck > age_of_chuck:
    print("请猜小一点,谢谢。")
else:
    print("你猜的太小了,请猜大一点。")

2、while猜3次循环

age_of_chuck = 25i = 0
while i <3:
_age_of_chuck = int(input("请输入chuck的年龄:"))
if _age_of_chuck == age_of_chuck: print("恭喜你猜对了") break elif _age_of_chuck > age_of_chuck: print("请猜小一点,谢谢。") else: print("你猜的太小了,请猜打一点。") i +=1 else: print("抱歉,你猜的次数太多了。")

3、for循环

age_of_chuck = 25

for i in range(3):
    _age_of_chuck = int(input("请输入chuck的年龄:"))
    if _age_of_chuck == age_of_chuck:
        print("恭喜你猜对了")
        break
    elif _age_of_chuck > age_of_chuck:
        print("请猜小一点,谢谢。")
    else:
        print("你猜的太小了,请猜大一点。")
else:
    print("抱歉,你猜的次数太多了。")

4、while进阶

age_of_chuck = 25

i = 0
while i <3:
    _age_of_chuck = int(input("请输入chuck的年龄:"))
    if _age_of_chuck == age_of_chuck:
        print("恭喜你猜对了")
        break
    elif _age_of_chuck > age_of_chuck:
        print("请猜小一点,谢谢。")
    else:
        print("你猜的太小了,请猜大一点。")
    i +=1
    if i == 3:
        confirm = input("是否继续:")
        if confirm != 'n':
            i = 0