python中的简单登录程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22170848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simple login program in python
提问by user3201916
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.
我已经用 python 编写了一个没有任何 GUI 的登录程序。我知道这是最简单的,但我不明白这个问题。
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
错误:输入登录 ID:admin 输入密码: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
采纳答案by Martijn Pieters
You used input()instead of raw_input()for the password.
您使用了input()代替raw_input()密码。
input()is the equivalent of eval(raw_input()); if you typed in adminfor the password it is interpreted as Python code. adminis then interpreted as a variable name, and because that name doesn't exist in your code, a NameErroris raised.
input()相当于eval(raw_input()); 如果您输入admin密码,它会被解释为 Python 代码。admin然后被解释为变量名,因为该名称在您的代码中不存在,所以NameError会引发 a。
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 selfparameter and are trying to reference a name lodthat doesn't exist. The following would work better:
接下来,您的check()方法将不起作用,因为您忘记了self参数并试图引用lod不存在的名称。以下方法效果更好:
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: "))
回答by Jonathan Coker
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()
回答by Jonathan Coker
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
好的,我不需要向 check 函数添加 id 和 pas 参数,因为我已经在 main 函数中使用了它。我宁愿调用 self 方法和用于身份验证的“if”
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()

