python猜年龄及用户登录作业(一)

一、猜年龄 , 可以让用户最多猜三次!

age = 18

count = 0
while count < 3:
    user_guess = int(input("input your guess: "))
    if user_guess > age:
        print("try smaller...")
    elif user_guess < age:
        print("try bigger...")
    else:
        print("you got it.")
    count += 1
else:
    print("you are foolish")

二、猜年龄 ,每隔3次,问他一下,还想不想继续玩,y,n

例1:

age = 18

count = 0
while count < 3:
    user_guess = int(input("input your guess: "))
    if user_guess > age:
        print("try smaller...")
    elif user_guess < age:
        print("try bigger...")
    else:
        print("you got it.")
    count += 1
    if count == 3:
        go_on = input("do you want to continue, y/n: ")
        if go_on == "y":
            count = 0
        elif go_on == "n":
            print("goodbye!!!")
            break
else:
    print("you are foolish")

例2:

age = 18

count = 0
while True:
    if count < 3:
        user_guess = int(input("input your guess: "))
        if user_guess > age:
            print("try smaller...")
        elif user_guess < age:
            print("try bigger...")
        else:
            print("you got it.")
        count += 1
    elif count == 3:
        go_on = input("do you want to continue, y/n")
        if go_on == "y":
            count = 0
        elif go_on == "n":
            print("goodbye!!!")
            break
else:
    print("you are foolish")

 

posted @ 2017-03-27 17:01  luchuangao  阅读(216)  评论(0)    收藏  举报