1

so I'm very basic with coding and Python. I used to be much better but haven't used it in some time and I'm trying to get back into the swing of things. I'm trying to execute an If statement, where input is asked for a username and password, if correct it then goes to a "definition" I think it's called, where it then executes the code in def access()

Code:

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()

However, I get this error when the correct inputs are executed:

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
>>>

Any help will be appreciated, thank you.

tk0019
  • 37
  • 4

3 Answers3

1

You have wrong the scopes, also you need to add a parameter to the function access:

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()
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
1

You're trying to invoke access() before it is declared! This is because the code defining access() is placed after its declaration, so your code is essentially trying to find a function that doesn't exist (yet)! This is a scoping issue, and is fairly common!

The code below (by placing the definition of access() outside of main()) works:

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()

I also do not believe it is recommended to define a function within a function (for this type of project), rather you can create a class with various functions inside:

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()

Going forward, you'll have to implement some sort of security (the code above has NONE! so be sure not to use it for important things. DO NOT use dictionaries of users and passwords, that's just for show!).

Hope it helps rekindle your love of Python!

cosinepenguin
  • 1,545
  • 1
  • 12
  • 21
  • Appreciate the great detail, I'll be staring at this for some time now, thank you! edit: this is legendary and will help me improve greatly, you sir are the real MVP! – tk0019 Jul 08 '17 at 21:59
  • I hope my code isn't too difficult to understand! I have a feeling you'll figure it out! You can also look into this python package [werkzeug.security](http://flask.pocoo.org/snippets/54/). It's very simple and it can hash passwords so that you can implement some security! – cosinepenguin Jul 08 '17 at 22:08
  • Again, thank you! I remember when I was last using Python on a similar project I was looking for a good security option for passwords, this will help immensely. Have a blessed day. – tk0019 Jul 08 '17 at 22:16
0

The problem is that your are defining the function after you call it, this would do:

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()

(asuming you aren't just confusing the spaces and actually didn't wanted to define one function inside the other)

Ciamar
  • 21
  • 4
  • If you do it this way, it prints twice "welcome user", you can correct the code by extrapolating the function outside – developer_hatch Jul 08 '17 at 21:45
  • you can also just delete one of the access() calls, the one on the end doesn't make much sense to me, but since it was there I didn't erase. – Ciamar Jul 08 '17 at 21:49