Python题目1:猜年龄(for、if else和where)

在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了。3次机会,猜错退出。

age = 60

for i in range(3):
    guess_age = int(input("guess age:"))
    if guess_age == age:
        print("Congratulations, you got it !")
        break
    elif guess > age:
        print("think smaller!")
    else:
        print("think bigger!")
else:
    print("too many times.....")

在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了。3次机会,猜错再次给机会猜测。

age = 60

count = 0
while count < 3:
    guess_age = int(input("guess age:"))
    if guess_age == age:
        print("Congratulations, you got it !")
        break
    elif guess_age > age:
        print("think smaller!")
    else:
        print("think bigger!")
    count += 1
    if count == 3:
        countine_confirm = input("do you want to keep guessing?press n will exit")
        if countine_confirm !="n":
            count = 0

  

原文地址:https://www.cnblogs.com/rouge2017/p/8280318.html