python作业第七天

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

count = 1
while count < 11:
    if count == 7:
        count += 1
        continue
    print(count)
    count += 1

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

count = 1
 a = 0
 while count < 101:
     a += count
     count += 1
 print(a)

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

count = 1
while count < 101:
    if count % 2 == 1:
        print(count)
        count += 1
    else:
        count += 1

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

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

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

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

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

user_info = ['Lance', '123']
i = 0
while i < 3:
    user_name = input('请输入您的账号:')
    user_pwd = input('请输入您的密码:')
    if user_name == user_info[0] and user_pwd == user_info[1]:
        print('登陆成功')
        break
    else:
        i += 1
        if i == 3:
            print('连续三次输入错误,请稍后尝试')
            break
        print('账号密码第{x}次输入错误,请重新输入'.format(x=i))

7:猜年龄游戏

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

age_info = 18
i = 0
while i < 3:
    inp_age = input('请输入您所猜测的年龄:')
    if age_info == int(inp_age):
        print('恭喜您猜对了')
        break
    else:
        i += 1
        if i == 3:
            print('连续三次输入错误,自动退出程序')
            break
        print('第{x}次猜错,请重新输入'.format(x=i))

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

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

age_info = 18
i = 1
while i < 4:
    inp_age = input('请输入您所猜测的年龄:')
    if age_info == int(inp_age):
        print('恭喜您猜对了')
        break
    else:
        print('第{x}次猜错,请重新输入'.format(x=i))
        i += 1
        if i == 4:
            print('连续三次输入错误,用户是否还想继续尝试?')
            while True:
                reply = input('您的选择是(Y或者y继续/N或者n结束):')
                if reply == 'y' or reply == 'Y':
                    i = 1
                    break
                elif reply == 'N' or reply == 'n':
                    i = 4
                    print('游戏结束,请下次再玩')
                    break
                else:
                    i = 3
                    print('输入无效符号,请重新输入')
                    continue

猜你喜欢

转载自www.cnblogs.com/Lance-WJ/p/12448592.html