用户登录注册简洁版
【一】需求介绍
- 设定好用户名和密码,用户通过输入指定的用户名和密码进行登陆
- 最大尝试次数:用户最多尝试猜测3次
- 最大尝试次数后:如3次后,问用户是否继续登陆
- 如果回答Y或y,就再给3次机会,提示【还剩最后三次机会】
- 3次都猜错的话登录结束
- 如果回答N或n,登陆结束!
- 如果格式输入错误,提示【输入格式错误,请重新输入:】
- 如果猜对了,登陆结束!
【二】功能分析
-
设定用户名和密码
-
设定猜测次数
-
用户名和密码正确
- 登陆成功
-
用户名和密码错误
- 输入 y 进行二次尝试
- 输入 n 退出登陆,程序结束
【三】代码实现
【1】只实现用户名和密码校验
# 【1】设定正确的用户名和密码
true_user_pwd = 'dream|521'
# 【2】解压赋值 + 字符串切割
true_username, true_password = true_user_pwd.split('|')
# 【3】用户输入用户名和密码
username_input = input("请输入用户名 :>>>> ").strip()
password_input = input("请输入密码 :>>>> ").strip()
# 【4】校验输入的用户名和密码是否一致
if true_username == username_input and password_input == true_password:
print(f"用户 {username_input} :>>>> 登陆成功!")
else:
print(f"用户 {username_input} :>>>> 用户名或密码错误!")
【2】增加尝试次数
# 【1】设定正确的用户名和密码
true_user_pwd = 'dream|521'
# 【2】设定尝试次数(最多尝试3次)和初识尝试次数(1)
can_choice = 3
count = 1
# 【3】设置循环 --- 计数次数小于额定次数 为 True 循环进行,否则循环结束
while count <= can_choice:
# 【4】每尝试一次,循环次数 + 1 (可尝试次数 - 1 )
count += 1
# 【5】解压赋值 + 字符串切割
true_username, true_password = true_user_pwd.split('|')
# 【6】用户输入用户名和密码
username_input = input("请输入用户名 :>>>> ").strip()
password_input = input("请输入密码 :>>>> ").strip()
# 【7】校验输入的用户名和密码是否一致
if true_username == username_input and password_input == true_password:
print(f"用户 {username_input} :>>>> 登陆成功!")
else:
print(f"用户 {username_input} :>>>> 用户名或密码错误!")
print(f"还剩 :>>>> {can_choice + 1 - count} 次机会")
【3】增加重试机制
# 【1】设定正确的用户名和密码
true_user_pwd = 'dream|521'
# 【2】设定尝试次数(最多尝试3次)和初识尝试次数(1)
can_choice = 3
count = 1
# 【3】设置循环 --- 计数次数小于额定次数 为 True 循环进行,否则循环结束
while count <= can_choice:
# 【4】每尝试一次,循环次数 + 1 (可尝试次数 - 1 )
count += 1
# 【5】解压赋值 + 字符串切割
true_username, true_password = true_user_pwd.split('|')
# 【6】用户输入用户名和密码
username_input = input("请输入用户名 :>>>> ").strip()
password_input = input("请输入密码 :>>>> ").strip()
# 【7】校验输入的用户名和密码是否一致
if true_username == username_input and password_input == true_password:
print(f"用户 {username_input} :>>>> 登陆成功!")
else:
print(f"用户 {username_input} :>>>> 用户名或密码错误!")
print(f"还剩 :>>>> {can_choice + 1 - count} 次机会")
# 【8】增加重试机制 : 输入 Y 将尝试次数重置为1,否则直接退出
if count == can_choice + 1:
age_input = input("请输入是否继续尝试(Y/N) :>>>> ")
if age_input == 'Y' or age_input == 'y':
print(f"欢迎再次尝试!祝您成功!")
count = 1
elif age_input == 'N' or age_input == 'n':
print(f"感谢您的尝试!下次再会!")
break
else:
print(f"意料之外的错误哦!")
break
【四】参考代码
# 先思考 再写主体功能注释 最后动手
"""单用户模式"""
# 用户登录功能 获取用户输入 比对用户数据
# 1.定义类似于数据库的数据
source_data = 'jason|123'
# 8.添加一个计数器
count = 1
# 5.添加循环
while True:
# 9.判断当前尝试的次数
if count == 4:
choice = input('您已经尝试三次了 是否继续(y/n)>>>:').strip()
if choice == 'y':
count = 1
else:
print('欢迎下次使用')
break
# 2.获取用户输入的用户名和密码
username = input('username>>>:').strip()
# TODO:自己可以结合自己的需求添加更多的校验
if len(username) == 0:
print('用户名不能为空')
continue
password = input('password>>>:').strip()
if len(password) == 0:
print('密码不能为空')
continue
# 3.切割原始数据得到用户名和密码再比对
real_name, real_pwd = source_data.split('|') # real_name, real_pwd=['jason','123'] 解压赋值
# 4.校验用户名和密码
if username == real_name and password == real_pwd:
print("登录成功")
# 6.添加内层循环
while True:
cmd = input('请输入您的指令>>>:').strip()
# 7.添加一个结束条件
if cmd == 'q':
break
print('正在执行您的命令:%s' % cmd)
else:
print('用户名或密码')
# 10.没错一次 计数器加一
count += 1
本文来自博客园,作者:Chimengmeng,转载请注明原文链接:https://www.cnblogs.com/dream-ze/p/17857401.html