关于python第41课后作业再次向各位老师求助
本帖最后由 caeser 于 2022-6-15 19:40 编辑各位老师,再次求助:
登陆时如果输入密码错误,再次输入成功后,可以正常退出。如图:
但登陆时,如果没有密码输错,退出时,反而成了死循环了,一直出现登陆界面,如下图:
register_dict={'adm':'123'}
print("欢迎来到鱼C论坛")
def get_int(): # 获取用户指令函数
print("""
==============
1.注册
2.登录
3.退出
""")
global num
num = int(input("请输入指令: "))
while num > 0 :
if num == 1:
register()
elif num == 2:
login()
elif num == 3:
break
def login():#登录函数
login_name = input("请输入用户名:")
n=0
while n ==0:
for each_n in list(register_dict.keys()):
n=0
if login_name ==each_n:
n = 1
if n != 1:
print("该用户名不存在。","\n")
login_name = input("请重新输入用户名:")
login_password = input("请输入密码:")
p = 0
while p == 0:
foreach_p in register_dict:
v = register_dict.get(each_p)
if v == login_password:
print("恭喜,登录成功~")
get_int()
else:
p = 1
print("密码错误")
login_password = input("请重新输入密码:")
def register():# 注册函数
name = input("请输入用户名:")
password = input("请输入密码:")
encrypt(name,password)
def encrypt(x,y):# 存放用户名和密码函数
#keys = register_dict.keys()
for each in list(register_dict.keys()):
if each == x:
print("用户名已经存在:请重新注册")
register()
register_dict = y
print("恭喜,注册成功")
get_int()
get_int()
本帖最后由 jackz007 于 2022-6-15 21:23 编辑
register_dict = {'adm':'123'}
def login():
count = 3
while count:
name = input("请输入用户名:") . strip()
if name in register_dict . keys():
passwd = input("请输入密码:") . strip()
if register_dict == passwd:
print('用户 [' , name , '] 成功登录')
break
else:
print('密码错误')
else:
print('用户 [' , name , '] 不存在\n')
count -= 1
def register():
r = True
while r:
name = input("请输入用户名:") . strip()
if name and not name in register_dict . keys():
while r:
passwd = input("请输入密码:") . strip()
if passwd:
register_dict = passwd
print('成功创建用户 [' , name , ']\n')
r = False
else:
print('密码不可以为空\n')
else:
print('用户 [' , name , '] 已经存在\n')
r = False
def menu(): # 获取用户指令函数
while True:
print("""
==============
1.注册
2.登录
3.退出
""")
num = int(input("请输入指令: "))
if num == 1:
register()
elif num == 2:
login()
elif num == 3:
break
print("欢迎来到鱼C论坛")
menu() 用exit()不好吗?
register_dict={'adm':'123'}
print("欢迎来到鱼C论坛")
def get_int(): # 获取用户指令函数
print("""
==============
1.注册
2.登录
3.退出
""")
num = int(input("请输入指令: "))
while num > 0 :
if num == 1:
register()
elif num == 2:
login()
elif num == 3:
exit()
def login():#登录函数
login_name = input("请输入用户名:")
n=0
while n ==0:
for each_n in list(register_dict.keys()):
n=0
if login_name ==each_n:
n = 1
if n != 1:
print("该用户名不存在。","\n")
login_name = input("请重新输入用户名:")
login_password = input("请输入密码:")
p = 0
while p == 0:
foreach_p in register_dict:
v = register_dict.get(each_p)
if v == login_password:
print("恭喜,登录成功~")
get_int()
else:
p = 1
print("密码错误")
login_password = input("请重新输入密码:")
def register():# 注册函数
name = input("请输入用户名:")
password = input("请输入密码:")
encrypt(name,password)
def encrypt(x,y):# 存放用户名和密码函数
#keys = register_dict.keys()
for each in list(register_dict.keys()):
if each == x:
print("用户名已经存在:请重新注册")
register()
register_dict = y
print("恭喜,注册成功")
get_int()
get_int() 还在学习中,还看不懂那么多函数 while p == 0:
foreach_p in register_dict:
v = register_dict.get(each_p)
if v == login_password:
print("恭喜,登录成功~")
get_int()
return #加个结束语
else:
p = 1
print("密码错误")
login_password = input("请重新输入密码:") jackz007 发表于 2022-6-15 21:21
非常感谢老师的付出,您写的代码比我精炼多了。但我想要的是我的代码到底哪里出错了,谢谢! 临时号 发表于 2022-6-16 01:21
用exit()不好吗?
用exit(),退出的时候老是有一个提示,所以就么有用,谢谢!
页:
[1]