-2

I've just started to use classes and i decided to create a quick login program. the program should setup a user and let them login but i'm getting a NameError whenever I run the program I've checked the spelling and everything seems to be correct. Any help would be appreciated

 class User(object):

    def __init__(self, name1, name2 , age , username, passw):
        self.name1 = name1
        self.name2 = name2
        self.age = age
        self.username = username
        self.passw = passw

    def user_setup(self):
        try:
            name1 = str(input("Please enter your first name : "))
            try:
                name2 = str(input("Please enter your last name : "))
                try:
                    age = int(input("Please enter your age in years"))
                except ValueError:
                    print("Thats not allowed")
            except ValueError:
                    print("Thats not allowed")
        except ValueError:
                    print("Thats not allowed")
        self.username = self.name1[0:3] + str(self.age)
        return self.username


    def login(self):
        for i in range(4):
            loginUsername = input("Please enter your username : ")
            if loginUsername == self.username:
                loginPassword = input("Please enter your password : ")
                if loginPassword == self.passw:
                    print("You have loggged in as " + self.username)
                else:
                    print("invalid")
            else:
                print("invalid")
        print("You have entered your username or password incorrectly too many times")
        quit()

def main():

    menu = int(input("1.new user\n2.login\n-"))
    if menu == 1:
        user_setup()
        login()
    elif menu == 2:
        login()
        print("test")



main()

THE ERROR I GET WHEN I RUN THE PROGRAM:

1.new user
2.login
-1
Traceback (most recent call last):
  File "C:\Users\colin\Desktop\Python programs\Password generator.py", line 58, in <module>
    main()
  File "C:\Users\colin\Desktop\Python programs\Password generator.py", line 50, in main
    user_setup()
NameError: name 'user_setup' is not defined
DavidG
  • 24,279
  • 14
  • 89
  • 82
Colin.M
  • 1
  • 2

4 Answers4

0

Change user_setup() to User.user_setup() in main(), since user_setup() is a method of the User class, python won't find it as a normal function.

You would also want to change login() to User.login()

However, classes are rather used in another way, for example a new user would more often be made by combining your user_setup() and __init__() and then creating a user this way:

user1 = User(...)
wohe1
  • 755
  • 7
  • 26
0

user_setup is a method on the class so you will need to instantiate that first before you can use it:

user = User(name1='foo', name2='bar', age=21, username='foobar', password='secret')
user.user_setup()
grahamlyons
  • 687
  • 5
  • 15
0

user_setup() is a method of User. You need to create a User instance and then call the method like so

user = User(name1, name2, age, username, passw)
user.user_setup()

If you want to call it without instantiating first, make user_setup a classmethod and use like so:

User.user_setup()
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
0

You need to create a User instance, then call the appropriate method. Also, in your __init__ you need to give default value for argument name etc.

ankur09011
  • 453
  • 3
  • 12