day03 作业

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

i = 0
while i < 10:
   i += 1
    if i == 7:
        continue
    else:
        print(number)
# 我最初的代码
count = 1
while count < 7:
	print(count)
	count = count + 1
count = count + 1
while count < 11:
	print(count)
	count = count + 1

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

number = 0
number_sum = 0
while number < 101:
    number_sum += number
    number += 1
print(number_sum)
# 我最初的代码
n = 100
s = n*(n + 1)/2
print(s)

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

number = 0
while number < 50:
    print(number*2+1)
    number += 1
# 我最初的代码
i = 1
while i < 100:
    print(i)
    i += 2

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

number = 0
while number < 50:
    print(number*2+2)
    number += 1
# 我最初的代码
i = 0
while i <= 100:
    print(i)
    i += 2

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

number = 0
even_number = 0
odd_number = 0
while number < 50:
    even_number -= number*2
    odd_number += number*2+1
    number += 1
print(odd_number+even_number)
# 我最初的代码
x = 1
y = 0
a = 0
b = 0
while x <= 99:
    x += 2
    a += x
    y -= 2
    b += y
print(a+b)

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

username = 'qwe'
password = '123'

count = 0
flog = True
while flog:
    if count < 3:
        inp_user = input(">>>请输入用户名:")
        inp_pass = input(">>>请输入密码:")
        if inp_user == username and inp_pass == password:
            print("登陆成功")
            flog = False
        else:
            print("登陆失败")
            count += 1
    else:
        flog = False

7.猜年龄游戏

要求:

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

age = 18
flog = True
count = 0
while flog:
    if count < 3:
        print("猜数字游戏")
        inp_age = input("请输入年龄:")
        if int(inp_age) < age:
            print("小了,大一点儿!")
            count += 1
        elif int(inp_age) > age:
            print("大了,小一点儿")
            count += 1
        else:
            print("哎哟,不错哦!")
            flog = False
    else:
        flog = False

8.猜年龄游戏升级版

要求:

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

age = 21
flog = True
count = 0
while flog:
    if count < 3:
        print("猜数字游戏")
        inp_age = input("请输入年龄:")
        if int(inp_age) < age:
            print("小了,大一点儿!")
            count += 1
        elif int(inp_age) > age:
            print("大了,小一点儿")
            count += 1
        else:
            print("哎哟,不错哦!")
            flog = False
    else:
        while count == 3:
            inp_command = input("是否继续游戏(y/n)")
            if inp_command in ['y', 'Y']:
                count = 0
            elif inp_command in ['n', 'N']:
                count = 0
                flog = False
            else:
                print("")

9.编写用户登录接口

需求

  1.输入账号密码完成验证,验证通过后输出"登录成功"
  2.可以登录不同的用户
  3.同一账号输错三次锁定

选做题1:

针对题目9附加功能

  在程序一直运行的情况下,一旦锁定,则锁定5分钟后自动解锁

import time

all_users_dic = {
    'qwe': {'pwd': '123', 'count': 1, 'locked': False},
    'asd': {'pwd': '123', 'count': 1, 'locked': False},
    'zxc': {'pwd': '123', 'count': 1, 'locked': False},
}

flog = True
while flog:
    inp_name = input(">>>请输入用户名:")
    if all_users_dic.get(inp_name)['locked']:
        print('该账号已被锁定,五分钟后自动解锁')
        time.sleep(300)
        all_users_dic.get(inp_name)['locked'] = False
        all_users_dic.get(inp_name)['count'] = 1
        print("解锁成功")
    inp_pwd = input(">>>请输入密码:")
    if inp_name in all_users_dic:
        if inp_pwd == all_users_dic.get(inp_name).get('pwd'):
            print("登陆成功")
            flog = False
        else:
            print("登陆失败")
            all_users_dic.get(inp_name)['count'] += 1
            if all_users_dic.get(inp_name)['count'] > 3:
                all_users_dic.get(inp_name)['locked'] = True
    else:
        print("该用户不存在")

针对题目9升级需求:

  完成用户一旦锁定,无论程序是否关闭,都锁定5分钟

import time
import os

all_users_dic = {
    'qwe': {'pwd': '123', 'count': 1},
    'asd': {'pwd': '123', 'count': 1},
    'zxc': {'pwd': '123', 'count': 1},
}

flog = True

while flog:
    inp_name = input(">>>请输入用户名:")
    if os.path.exists(f'{inp_name}.txt'):
        print("该用户已被锁定,五分钟后自动解锁")
        time.sleep(10)
        print("解锁成功!")
        # 解锁成功,则删除文件
        os.remove(f'{inp_name}.txt')
        all_users_dic.get(inp_name)["count"] = 1
    inp_pwd = input(">>>请输入密码:")
    if inp_name in all_users_dic:
        if inp_pwd == all_users_dic.get(inp_name)["pwd"]:
            print(f"欢迎,{inp_name}")
            # 后续功能模块
            flog = False
        else:
            if all_users_dic.get(inp_name)["count"] == 3:
                print("登录失败,用户已被锁定")
                # 如果失败次数等于三次,则锁定用户
                with open(f'{inp_name}.txt', 'w', encoding='utf-8') as f:
                    f.write('')
            else:
                print(f"登录失败,还可尝试{3 - all_users_dic.get(inp_name)['count']}次")
                all_users_dic.get(inp_name)["count"] += 1
    else:
        print("该用户不存在")
posted @ 2020-02-25 15:27  轻描丨淡写  阅读(179)  评论(0编辑  收藏  举报