Discuss / Python / 弱弱问一句,这个问题是不是很低级啊?小白求指点

弱弱问一句,这个问题是不是很低级啊?小白求指点

Topic source

upbeat_peach

#1 Created at ... [Delete] [Delete and Lock User]

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~~~")

夏漾年华

#2 Created at ... [Delete] [Delete and Lock User]

User这个类的初始化函数你没加双下划线,应该是__init__。我感觉是这里有问题。不知道对不对


  • 1

Reply