3

I have written a program in python for login without any GUI. I know it is the simplest one but i don't understand the problem.

class login:
    def __init__(self,id,pas):
        self.id="admin"
        self.pas="admin"

    def check(id,pas):
        print self.id
        print lod.id
        if(self.id==log.id and self.pas==log.pas):
            print "Login success!"


log=login("","")
log.check(raw_input("Enter Login ID:"),
        input("Enter password: "))

print "Login Page" 

Error: Enter Login ID:admin Enter password: admin

Traceback (most recent call last):
  File "C:/Python27/login.py", line 15, in <module>
    input("Enter password: "))
  File "<string>", line 1, in <module>
NameError: name 'admin' is not defined

3 Answers 3

8

You used input() instead of raw_input() for the password.

input() is the equivalent of eval(raw_input()); if you typed in admin for the password it is interpreted as Python code. admin is then interpreted as a variable name, and because that name doesn't exist in your code, a NameError is raised.

Use:

log.check(raw_input("Enter Login ID:"),
          raw_input("Enter password: "))

instead.

Next up, your check() method won't work, as you forgot the self parameter and are trying to reference a name lod that doesn't exist. The following would work better:

class login:
    def __init__(self, id, pas):
        self.id = id
        self.pas = pas

    def check(self, id, pas):
        print self.id
        if self.id == id and self.pas == pas:
            print "Login success!"

log = login("admin", "admin")
log.check(raw_input("Enter Login ID:"),
          raw_input("Enter password: "))
Sign up to request clarification or add additional context in comments.

7 Comments

Martijn, that is only one part. The class definition is a mess.
@Matthias: first things first. :-)
You're right. That was the reason for the NameError. Let's wait and look what happens now.
@MartijnPieters I have one question.. If i want asterisk while taking password instead of plain text, How can it be done. Also how i compare it.
@user3201916: use the getpass.getpass() function instead of raw_input() to take passwords. It won't print asterisks in that case, but the password will be hidden.
|
0

Alright, there was no need for me to add id and pas argument to the check function since I already have it in the main function. I rather called self method and the "if" for authentication

class Login:
    error = None
    def __init__(self, uid, passw):
        self.uid = "admin"
        self.passw = "admin"
        Login.error = "Enter a valid user id and password"

    def authenticate(self):
        if (self.uid == logid and self.passw == logpass):
            print ("Login successful")
        else:
            print (Login.error)
log = Login("", "")
logid = input("Enter your user ID: ")
logpass = input("Enter your password: ")


log.authenticate()

1 Comment

In future, to format your code you can highlight all of it and press the format code button in the editor, it looks like this: {}
0

Try this.

class Login:
    def __init__(self, id, password):
        self.id = id
        self.password = password
        self.error = "Enter a valid username and password"
    def check(self):
        if (self.id == log_id and self.password == log_pass):
            print("Login successful")
        else:
            print(self.error)

log = Login("admin",  "admin")
log_id = input("Enter your user ID: ")
log_pass = input("Enter password: ")
log.check()

3 Comments

Hi and welcome to StackOverflow! To post code, please prepend each line with four spaces. Also, it would be nice if you gave some explanation to your code. Answers that are only a piece of code are usually not very well received around here.
class Login: error = None def __init__(self, uid, passw): self.uid = "admin" self.passw = "admin" Login.error = "Enter a valid user id and password" def authenticate(self): if (self.uid == logid and self.passw == logpass): print ("Login successful") else: print (Login.error) log = Login("", "") logid = input("Enter your user ID: ") logpass = input("Enter your password: ") log.authenticate()
class Login: error = None def __init__(self, uid, passw): self.uid = "admin" self.passw = "admin" Login.error = "Enter a valid user id and password" def authenticate(self): if (self.uid == logid and self.passw == logpass): print ("Login successful") else: print (Login.error) log = Login("", "") logid = input("Enter your user ID: ") logpass = input("Enter your password: ") log.authenticate()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.