class User:
def __int__(self, username, password):
self.username = username
md5 = hashlib.md5(password.encode())
self.password = md5.hexdigest()
def check_password(self, password):
md5 = hashlib.md5(password.encode())
if md5.hexdigest() == self.password:
return "密码正确"
else:
return "密码错误"
user = User("li", 12345678)
print("加密后的密码:", User.password)
print(User.check_password("123456"))
print(User.check_password("12345678"))
运行结果:
user = User("li", "12345678")
TypeError: User() takes no arguments
问题1: 第四行应该是init 你写的int
问题2:18-20行,调用应该是user而不是User
import hashlib
class User:
def __init__(self, username, password):
self.username = username
md5 = hashlib.md5(password.encode())
self.password = md5.hexdigest()
def check_password(self, password):
md5 = hashlib.md5(password.encode())
if md5.hexdigest() == self.password:
return "密码正确"
else:
return "密码错误"
user = User("li", "12345678")
print("加密后的密码:", user.password)
print(user.check_password("123456"))
print(user.check_password("12345678"))