第一周练习题

  • 1.实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!
tag = True
while tag:
    name = input('please your name:')
    psw = input('please your password:')
    if name == 'seven' and psw == '123':
        print('login successful...')
    else:
        print('user or password err...')
  • 实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
tag = True
count = 0
while tag:
    if count == 3:
        print('Too many errors...')
        break
    name = input('please your name:')
    psw = input('please your password:')
    if name == 'seven' and psw == '123':
        print('log in successful...')
        tag = False
    else:
        print('user or password err...')
        count += 1

print('...Its over')
  • 3.实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
#方法一:
tag = True
i = 0
j = 0
while tag:
    if i == 3 or j == 3:
        print('Too many errors...')
        break
    name = input('please your name:')
    psw = input('please your password:')
    if name == 'seven':
        if psw == '123':
            print('log in successful...')
        else:
            print('password err...')
            i += 1
    elif name == 'alex':
        if psw == '123':
            print('log in successful...')
        else:
            print('password err...')
            j += 1
    else:
        print('user err...')
print('---Its over---')
'''这代码感觉太low了 '''
#方法二:
dic = {
    'seven': {'psw': '123', 'count': 0},
    'alex': {'psw': '456', 'count': 0},
    'wood':{'psw':'000','count':0}
}
tag = True
while tag:
    user_name = input('please your name:')
    if not user_name in dic:
        print('...No username exists...')
        continue
    if dic[user_name]['count'] == 3:
        print('...Too many errors...')
        continue
    user_psw = input('please your password:')
    if user_psw == dic[user_name]['psw']:
        print('...log in successful...')
        print('''请进行操作:
                            1.浏览列表
                            2.选定对象
                            3.开始聊天
                            ''')
        while tag:
            choose = input('>>:')
            if choose == '1':
                print('浏览列表中。。。。。。')
            elif choose == '2':
                print('对象以选定。。。。。。')
            elif choose == '3':
                print('聊天中。。。。。。')
            elif choose == 'quit':
                tag=False
            else:
                print('指令无效,请重新输入。。。')
    else:
        print('...password err...')
        dic[user_name]['count'] += 1
print('---Its over---')

  • a.使用while循环实现输出2-3+4-5+6…+100 的和
count = 3
i = 2
j = 3
while count <= 100:
    if count % 2 == 0:
        i = i + j
    else:
        i = i - j
    j += 1
    count += 1
print(i)
  • 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12 使用 while 循环实现输出 1-100 内的所有奇数
count = 0
while count <= 100:
    if count % 2 != 0:
        print(count)
    count += 1

  • 使用 while 循环实现输出 1-100 内的所有偶数
count = 1
while count <= 100:
    if count % 2 == 0:
        print(count)
    count += 1
  • 基础需求:
    1.让用户输入用户名密码
    2.认证成功后显示欢迎信息
    3.输错三次后退出程序
dic = {
    'wood': {'psw': '123', 'count': 0},
    'egon': {'psw': '456', 'count': 0},
    'alex': {'psw': '789', 'count': 0}
}
tag = True
while tag:
    name = input('please your username:')
    if not name in dic:
        print('用户名不存在')
        continue
    if dic[name]['count'] == 3:
        print('尝试次数过多,锁定')
        continue
    psw = input('please your password:')
    if psw == dic[name]['psw']:
        print('login successful...')
        tag = False
    else:
        print('password err...')
        dic[name]['count'] += 1
  • 升级需求:

1.可以支持多个用户登录 (提示,通过列表存多个账户信息)
2.用户3次认证失败后,退出程序,再次启动程序尝试登录时,
还是锁定状态(提示:需把用户锁定的状态存到文件里)

dic={
    'egon1':{'password':'123','count':0},
    'egon2':{'password':'123','count':0},
    'egon3':{'password':'123','count':0},
}

count=0
while True:
    name=input('u>>: ')
    if name not in dic:
        print('用户不存在')
        continue

    with open('db.txt','r') as f:
        lock_users=f.read().split('|')
        if name  in lock_users:
            print('用户%s已经被锁定' %name)
            break

    if dic[name]['count'] > 2:
        print('尝试次数过多,锁定')
        with open('db.txt','a') as f:
            f.write('%s|' %name)
        break

    password=input('p>>: ')

    if password == dic[name]:
        print('登录成功')
        break
    else:
        print('用户名或密码错误')
        dic[name]['count']+=1

你可能感兴趣的:(Python)