模拟登录和修改密码——Python实现

 

 

"""
写一个登录类,这个类里有一个属性,存放着账号密码
实例化这个类会生成一段四位数随机验证码
这个类有两个方法:
·登录方法,账号密码和属性中的账号密码和验证码是否一致,一致就返回成功
·修改密码,接收账号密码和新密码,如果账号密码和验证码一致,则可以将属性中的对应的密码修改为新密码
"""
import random

class LoginSystem:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        print(self.username)
        print(self.password)
        self.verfication_code = random.randint(1000,9999)
    def login(self, username, password, verfication_code):
        if username == self.username and password == self.password and self.verfication_code == verfication_code:
            return "登录成功"
        else:
            return "登录失败"
    def change_password(self, username, password, new_password, verfication_code):
        if username == self.username and password == self.password and verfication_code == self.verfication_code:
            self.password = new_password
            return "修改密码成功"
        else:
            return "修改密码失败"

login_system = LoginSystem("root", "1234")
code = login_system.verfication_code
print("验证码:", code)

"""登录测试"""
username = input("用户名:")
password = input("密码:")
verification_code = int(input("请输入验证码:"))
login_result = login_system.login(username,password,verification_code)
print("登录结果", login_result)

"""修改密码测试"""
# username = input("用户名:")
# password = input("密码:")
# new_password = input("请输入新的密码:")
# verification_code = int(input("请输入验证码:"))
# login_result = login_system.change_password(username,password,new_password,verification_code)
# print("修改密码结果:", login_result)

 

登录测试和修改密码测试,可以单独执行。

 

posted @ 2024-05-07 10:58  宝山方圆  阅读(112)  评论(0编辑  收藏  举报