day06作业

作业(必做题):
#1. 使用while循环输出1 2 3 4 5 6 8 9 10

count=0
while count<10:
    count+=1
    print(count)

#2. 求1-100的所有数的和

count=0
sum_count=0
while count<100:
    count+=1
    sum_count+=count
else:
    print("100以内和:{}".format(sum_count))

#3. 输出 1-100 内的所有奇数

count=0
while count<100:
    count+=1
    if count%2==1:
        print(count)

#4. 输出 1-100 内的所有偶数

count=0
while count<100:
    count+=1
    if count%2==0:
        print(count)

#5. 求1-2+3-4+5 ... 99的所有数的和

count=0
sum_count=0
while count<99:
    count+=1
    if count%2==1:
        sum_count+=count
    else:
        sum_count-=count
else:
    print(sum_count)

#6. 用户登陆(三次机会重试)

user_name="egon"
user_pwd="123"

count=0
while count<3:
    name_inp = input("请输入用户名:")
    pwd_inp = input("请输入密码:")
    if name_inp == user_name and pwd_inp == user_pwd:
        print("登入成功")
        break
    else:
        print("账号或密码错误,请重试。")
        count+=1
else:
    print("输入错误三次,请稍后重试。")

#7:猜年龄游戏
要求:
允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出

egon_age=66
count=0
while count<3:
    user_inp = input("请输入年龄:")
    if user_inp == egon_age:
        print("恭喜猜中。")
        break
    elif user_inp > egon_age:
        print("猜错了,有点偏大了。")
        count+=1
    else:
        print("猜错了,有点偏小了。")
        count += 1
else:
    print("猜错三次,请稍后重试。")

#8:猜年龄游戏升级版(选做题)
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出

egon_age=66
count=0
while count<3:
    user_inp = int(input("请输入年龄:").strip())
    if user_inp == egon_age:
        print("恭喜猜中。")
        break
    elif user_inp > egon_age:
        print("猜错了,有点偏大了。")
        count+=1
    else:
        print("猜错了,有点偏小了。")
        count += 1
    if count == 3:
        user_choice=input("已猜错三次,是否继续Y/N,y/n:")
        if user_choice == "Y" or user_choice == "y":
            count=0
        elif user_choice == "N" or user_choice == "n":
            break
        else:
            print("请输入y/n")
else:
    print("程序退出。")
原文地址:https://www.cnblogs.com/baicai37/p/12449782.html