鱼C论坛

 找回密码
 立即注册
查看: 539|回复: 2

[已解决]26课 动动手 萌新求助

[复制链接]
发表于 2019-6-22 18:14:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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!
最佳答案
2019-6-23 16:13:29
原因在于这一句  account = {name:password},所以第二次创建就会将原来的用户密码覆盖了,导致account里面只能存储一个,

正确的方式是这样:
account [name] = password
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-23 16:13:29 | 显示全部楼层    本楼为最佳答案   
原因在于这一句  account = {name:password},所以第二次创建就会将原来的用户密码覆盖了,导致account里面只能存储一个,

正确的方式是这样:
account [name] = password
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-6-26 11:25:03 | 显示全部楼层
newu 发表于 2019-6-23 16:13
原因在于这一句  account = {name:password},所以第二次创建就会将原来的用户密码覆盖了,导致account里面 ...

非常感谢您的帮助
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 05:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表