Traceback (most recent call last):
File "D:/practice/liaoxuefeng/0430hashlib.py", line 39, in <module>
word = (username.password == get_md5(password + username.salt))
AttributeError: 'str' object has no attribute 'password'
class User(object):
global username, password,db#不管有没有这句,报错的内容不变
def init(self,username,password):
self.username = username
self.salt = ''.join([chr(random.randint(48,122)) for i in range(20)])
self.password = get_md5(password + self.salt)
def login(username, password):
user = db[username]
if user not in db:
return False
word = (username.password == get_md5(password + username.salt))
if word == True:
print("Welcome back, %s!" %(username))
else:
print("Please try again later~~~")
upbeat_peach
Traceback (most recent call last): File "D:/practice/liaoxuefeng/0430hashlib.py", line 39, in <module> word = (username.password == get_md5(password + username.salt)) AttributeError: 'str' object has no attribute 'password'
import hashlib,random
db = { 'michael': 'e10adc3949ba59abbe56e057f20f883e', 'bob': '878ef96e86145580c38c87f0410ad153', 'alice': '99b1c2188db85afee403b1536010c2c9' } username = input("请输入用户名:") password = str(input("请输入密码:"))
def get_md5(s): return hashlib.md5(s.encode('utf-8')).hexdigest()
class User(object): global username, password,db#不管有没有这句,报错的内容不变 def init(self,username,password): self.username = username self.salt = ''.join([chr(random.randint(48,122)) for i in range(20)]) self.password = get_md5(password + self.salt)
def login(username, password): user = db[username] if user not in db: return False word = (username.password == get_md5(password + username.salt))
if word == True: print("Welcome back, %s!" %(username)) else: print("Please try again later~~~")