|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
0. 尝试编写一个用户登录程序(这次尝试将功能封装成函数),程序实现如图:
===================== RESTART: C:/Users/猪/Desktop/456.py =====================
|--- 新建用户:N/n ---|
|--- 登录账号:E/e ---|
|--- 推出程序:Q/q ---|
|--- 请输入指令代码:N
请输入用户名:小甲鱼
请输入密码:FishC
注册成功,赶紧试试登录吧^_^
|--- 新建用户:N/n ---|
|--- 登录账号:E/e ---|
|--- 推出程序:Q/q ---|
|--- 请输入指令代码:n
请输入用户名:小甲鱼
此用户名已经被使用,请重新输入:小鱿鱼
请输入密码:FishC
注册成功,赶紧试试登录吧^_^
|--- 新建用户:N/n ---|
|--- 登录账号:E/e ---|
|--- 推出程序:Q/q ---|
|--- 请输入指令代码:E
请输入用户名:小小鱼
您输入的用户名不存在,请重新输入:小甲鱼
请输入密码:FishC
欢迎进入XXOO系统,请点右上角的X结束程序!
我的代码:
print('|---Cream account:N/n---|')
print('|---Log in account:E/e---|')
print('|---Exit ---|')
account = {}
while 1:
code = input('Pls input code:')
if code == 'N' or code == 'n':
name = input('Pls input user name:')
while name in account:
name = input('The user name is existed. Pls input another:')
if name not in account:
password = input('Pls input your password:')
account = {name:password}
print('Registed successfully!')
if code == 'E' or code == 'e':
name = input('Pls input user name:')
while name not in account:
name = input('The user is not existed. Pls input again:')
if name in account:
password = input('Pls input your password:')
pw = account.get(name)
if password == pw:
print('Welcome to system!')
while password != pw:
password = input('Your password is incorrect. Pls input again:')
if code == 'Q' or code == 'q':
print('Thank you for using.')
break
我的问题:
如果像答案里的先注册一个'小甲鱼'再注册一个'小鱿鱼',如果我想登陆'小甲鱼’的账号的话,会显示这个账号不存在。如下:
|---Cream account:N/n---|
|---Log in account:E/e---|
|---Exit program---|
Pls input order code:n
Pls input user name:小甲鱼
Pls input your password:fishc
Registed successfully!
Pls input order code:n
Pls input user name:小甲鱼
The user name is existed. Pls input another:小鱿鱼
Pls input your password:fishc
Registed successfully!
Pls input order code:e
Pls input user name:小甲鱼
The user is not existed. Pls input again:小鱿鱼
Pls input your password:fishc
Welcome to system!
原因在于这一句 account = {name:password},所以第二次创建就会将原来的用户密码覆盖了,导致account里面只能存储一个,
正确的方式是这样:
account [name] = password
|
|