Python-在字典中打印多个内容时出现问题
我有一些非常基本的帐户登录系统的代码。问题是我有三个部分组成一个帐户:Python-在字典中打印多个内容时出现问题,python,dictionary,Python,Dictionary,我有一些非常基本的帐户登录系统的代码。问题是我有三个部分组成一个帐户:uname,upass,然后是bal。在我引入bal之前,一切都很好 我对Python非常业余,不知道如何修复它。我的意图是在我添加的每个帐户上都有三个部分,用户名、密码,然后是帐户余额,但到目前为止,我已经用我的知识编写了尽可能符合逻辑的代码,以使其正常工作,但它似乎无法很好地打印或使用余额 代码: 错误: Traceback (most recent call last): File "C:\Users\Tom\Des
uname
,upass
,然后是bal
。在我引入bal
之前,一切都很好
我对Python非常业余,不知道如何修复它。我的意图是在我添加的每个帐户上都有三个部分,用户名、密码,然后是帐户余额,但到目前为止,我已经用我的知识编写了尽可能符合逻辑的代码,以使其正常工作,但它似乎无法很好地打印或使用余额
代码:
错误:
Traceback (most recent call last):
File "C:\Users\Tom\Desktop\test.py", line 29, in <module>
main()
File "C:\Users\Tom\Desktop\test.py", line 27, in main
mylogin.login()
File "C:\Users\Tom\Desktop\test.py", line 14, in login
self.access(userNameInput)
TypeError: access() missing 1 required positional argument: 'bal'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Tom\Desktop\test.py”,第29行,在
main()
文件“C:\Users\Tom\Desktop\test.py”,第27行,在main中
mylogin.login()
文件“C:\Users\Tom\Desktop\test.py”,第14行,登录
self.access(userNameInput)
TypeError:access()缺少1个必需的位置参数:“bal”
特别次要问题:
对于“Def access”部分,
uname
部分由于某种原因可以命名为任何名称,但在登录时仍然打印帐户的实际名称。对于我来说,这毫无意义,为什么那部分可以用完全不同的名称完美地工作。bal
从未用该名称保存过。改用str(self.users[uname][1])
回答第二个问题,只需打印参数的值,而不进行任何检查。你为什么期望它失败?但是,当您实现此答案第一部分中建议的更改时,它将失败。问题是
access
方法将bal
作为参数,但在调用self.access(userNameInput)
时,您没有传递bal
。您可以在access
方法中获取bal
,然后删除bal
参数
此外,您不能在此处连接字符串和整数:print(“欢迎,+uname+”!“+bal)
。只需使用str.format
即可
class Login:
def __init__(self):
self.users = {}
def addUser(self, uname, upass, bal):
self.users[uname] = upass, bal
def login(self):
userNameInput = input("Username: ")
userPassInput = input("Password: ")
if userNameInput in self.users:
if userPassInput in self.users[userNameInput]:
print("Access Granted!")
self.access(userNameInput)
else:
print("Access Denied!")
# You shouldn't recurse here and use a loop instead.
# Python has a recursion limit.
return self.login()
# Remove the `bal` parameter.
def access(self, uname):
# Get the bal out of the self.users dict.
bal = self.users[uname][1]
print("Welcome, {}!{}".format(uname, bal))
def main():
mylogin = Login()
mylogin.addUser("u123", "p123", 123)
mylogin.login()
main()
我想您忘记在access中传递bal参数了function@KalyanReddy对不起,我应该说我试过了。然后它简单地给了我一个新的错误:TypeError:access()缺少1个必需的位置参数:“bal”如果添加参数bal,那么应该在函数调用中发送它
class Login:
def __init__(self):
self.users = {}
def addUser(self, uname, upass, bal):
self.users[uname] = upass, bal
def login(self):
userNameInput = input("Username: ")
userPassInput = input("Password: ")
if userNameInput in self.users:
if userPassInput in self.users[userNameInput]:
print("Access Granted!")
self.access(userNameInput)
else:
print("Access Denied!")
# You shouldn't recurse here and use a loop instead.
# Python has a recursion limit.
return self.login()
# Remove the `bal` parameter.
def access(self, uname):
# Get the bal out of the self.users dict.
bal = self.users[uname][1]
print("Welcome, {}!{}".format(uname, bal))
def main():
mylogin = Login()
mylogin.addUser("u123", "p123", 123)
mylogin.login()
main()