作业 —— day07

作业(必做题):

1.使用while循环输出1 2 3 4 5 6 8 9 10

i = 0
while i < 10:
    i += 1
    if i == 7:
        continue
    print(i)

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

i = 1
sum = 0
while i <= 100:
    sum += i
    i +=1
print(sum)

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

i = 1
while i < 100:
    print(i)
    i += 2

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

i = 0
while i < 100:
    i += 2
    print(i)

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

single = 1
double = -2
sum = 0
while single < 100:
    single += 2
    sum = sum + single + double
    double -= 2
print(sum)

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

username = 'xxq'
password = '123'
count = 0
rest = 3
while count < 3:
    inp_name = input('请输入帐号:')
    inp_pwd = input('请输入密码:')
    if inp_name == username and inp_pwd == password:
        print('登录成功!')
        break
    else:
        count += 1
        rest -= 1
        print('您已输错',count,'次')
        print('您还剩余',rest,'次')
else:
    print('输错3次,退出程序')

7.猜年龄游戏

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

age = 18
count = 0
rest = 3
while count < 3:
    inp_age = input('请输入猜测的年龄:')
    if inp_age.isdigit():
        inp_age = int(inp_age)
        if inp_age == age:
            print('恭喜你!猜对啦!')
            break
        else:
            count += 1
            rest -= 1
            print('您已猜错',count,'次')
            print('您还剩余',rest,'次')
    else:
        print('请输入数字')
else:
    print('输错3次,退出程序')

8.猜年龄游戏升级版(选做题)

要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序,如果猜对了,就直接退出

age = 18
count = 0
while count < 3:
    inp_age = input('请输入猜测的年龄:')
    if inp_age.isdigit():
        inp_age = int(inp_age)
        count += 1
        if inp_age == age:
            print('恭喜你!猜对啦!')
            break
        elif count == 3:
            msg = input("是否继续 Y/y or N/n: ")
            if msg == "y" or msg == "Y":
                print("再来亿次")
                count = 0
                continue
            elif msg == "N" or msg == "n":
                print("GG了")
                break
            else:
                print("错了,再来")
        else:
            print('错',count,'次了')
    else:
        print('请输入数字')
posted @ 2020-03-09 20:43  轻描丨淡写  阅读(235)  评论(0编辑  收藏  举报