If语句到定义时出错((Python)

2024-04-30 03:00:37 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我对编码和Python非常熟悉。我以前好多了,但已经有一段时间没用了,我正在努力重新适应生活。我试图执行一个If语句,在这里输入一个用户名和密码,如果正确的话,它会进入一个“定义”,我想它会被调用,然后执行def access()

代码:

def main():

    userName = ("u123")
    userPass = ("p123")

    userNameInput = input("Username: ")
    userPassInput = input("Password: ")

    if userPassInput == userPass and userNameInput == userName:
        print("Access granted")
        access()

    else:
        print("Access denied")
        return main()

    def access():

        print("Welcome, " + userName)

    access()

main()

但是,当执行正确的输入时,我会出现以下错误:

Username: u123
Password: p123
Access granted
Traceback (most recent call last):
  File "C:/Users/Tom/Desktop/test.py", line 23, in <module>
    main()
  File "C:/Users/Tom/Desktop/test.py", line 11, in main
    access()
UnboundLocalError: local variable 'access' referenced before assignment
>>>

任何帮助都将不胜感激,谢谢


Tags: inputaccessmaindefusernamepasswordprintuserpass
3条回答

您试图在声明access()之前调用它!这是因为定义access()的代码放在它的声明之后,所以您的代码实际上是在试图找到一个不存在的函数!这是一个scoping issue,非常常见

下面的代码(通过将access()的定义置于main()之外)起作用:

def main():

    userName = ("u123")
    userPass = ("p123")

    userNameInput = input("Username: ")
    userPassInput = input("Password: ")

    if userPassInput == userPass and userNameInput == userName:
        print("Access granted")
        access(userName)

    else:
        print("Access denied")
        return main()

def access(userName):
    print("Welcome, " + userName)

main()

我也不认为建议在函数中定义函数(对于这种类型的项目),而是可以创建一个包含各种函数的类:

class Login:
    def __init__(self):
        self.users = {}
        self.userName = ("u123")
        self.userPass = ("p123")

    def addUser(self, uname, upass):
        self.users[uname] = upass;

    def login(self):
        userNameInput = input("Username: ")
        userPassInput = input("Password: ")
        if userNameInput in self.users:
            if self.users[userNameInput] == userPassInput:
                print("Access granted")
                self.access(userNameInput)
            else:
                print("Access denied")
                return self.login()
        else:
            print("Access denied")
            return self.login()

    def access(self, username):
        print("Welcome, "+username+"!")


def main():
    mylogin = Login()
    mylogin.addUser("u123","p123")
    mylogin.login()

main()

接下来,您必须实现某种安全性(上面的代码没有任何安全性!所以千万不要把它用在重要的事情上。不要使用用户和密码字典,那只是作秀而已

希望它能重新点燃你对Python的爱

问题是,在调用函数之后,如果您正在定义该函数,则会执行以下操作:

def main():

  userName = ("u123")
  userPass = ("p123")

  userNameInput = input("Username: ")
  userPassInput = input("Password: ")

  def access():

      print("Welcome, " + userName)

  if userPassInput == userPass and userNameInput == userName:
      print("Access granted")
      access()

  else:
      print("Access denied")
      return main()



  access()

main()

(假设你不只是混淆了空格,实际上不想在另一个函数中定义一个函数)

作用域错误,还需要向函数访问中添加一个参数:

def main():

  userName = ("u123")
  userPass = ("p123")

  userNameInput = input("Username: ")
  userPassInput = input("Password: ")

  if userPassInput == userPass and userNameInput == userName:
    print("Access granted")
    #now you are able to use the function "access"
    access(userName)

  else:
    print("Access denied")
    return main()

#here, define the function in the same identation of the main function, not inside it
def access(userName):
  print("Welcome, " + userName)



main()

相关问题 更多 >