编写简单的python密码重置代码示例时遇到麻烦

我是Python的新手,它试图编写一个简单的密码重置代码示例以了解集合,字典和异常处理程序。要重置密码,程序必须首先同时输入student_id和user_id并根据列表确认其ID。如果student_id和user_id匹配,它将提示用户输入其入学期限,作为一种额外的安全措施。如果所有内容都匹配,该程序将按名称向用户打招呼,并提示他们输入新密码。新密码不能与用户以前的任何密码匹配。在用户要求退出或成功登录之前,程序不应退出(请注意,重设密码不应是最后一次,如果他们确定不想更改密码,我们可以为用户提供“退出”选项。)

程序应类似于以下内容运行:

1. Login
2. Reset Password
3. Quit
What would you like to do?  1
User Name: afrank
Password: xyz123
Login Success!

------------------------------------------------
1. Login
2. Reset Password
3. Quit
What would you like to do? 1
User Name: afrank
Password: xyz1234
Incorrect Password
1. Login
2. Reset Password
3. Quit
What would you like to do? 3

------------------------------------------------
1. Login
2. Reset Password
3. Quit
What would you like to do? 2
Student ID: 392111111
User Name: afrank
What year were you admitted? 2018
New Password: abc456
Confirm New Password: abc456
Password Changed!
1. Login
2. Reset Password
3. Quit
What would you like to do? 3


------------------------------------------------
1. Login
2. Reset Password
3. Quit
What would you like to do? 2
Student ID: afrank
Error! Please enter your Student ID Number.
Student ID: 39211111x
Error! Please enter your Student ID Number.
Student ID: 39211111111
Error! Student ID Not Found
Student ID: 392111111
User Name: afrank
What year were you admitted? 2018
New Password: abc456
Confirm New Password: abc456
Password Changed!
1. Login
2. Reset Password
3. Quit
What would you like to do? 3

------------------------------------------------
1. Login
2. Reset Password
3. Quit
What would you like to do? 2
Student ID: 392111111
User Name: afrank
What year were you admitted? 2018
New Password: 123abc
Confirm New Password: 123abc
Error. Your password cannot be a previously used password.
New Password: abc456
Confirm New Password: abc456
1. Login
2. Reset Password
3. Quit
What would you like to do? 1
User Name: afrank
Password: abc456
Login Success!

编写简单的python密码重置代码示例时遇到麻烦

我担心了一些,但我不知道如何继续:

idfile = open('student_password.csv','r')     ## Read in Current File
header = idfile.readline()
allobs = idfile.readlines()
namedict = {}
userdict = {}
termdict = {}
pwdict = {}
p_pwdict = {}
for obs in allobs:
    obslist=obs.split(",")
    namedict2 = {int(obslist[0]): (obslist[1],obslist[2])}
    userdict2 = {int(obslist[0]): obslist[3]}
    termdict2 = {int(obslist[0]): int(obslist[4])}
    pwdict2 = {int(obslist[0]): obslist[5]}
    p_pwdict2 = {int(obslist[0]): obslist[6].replace('\n','')}
    namedict.update(namedict2)
    userdict.update(userdict2)
    termdict.update(termdict2)
    pwdict.update(pwdict2)
    p_pwdict.update(p_pwdict2)
idfile.close()

result = input(' 1. Login \n 2. Reset Password \n 3. Quit \n What would you like to do? \n ')

if result == '1':
...



piaodongdeyun1225 回答:编写简单的python密码重置代码示例时遇到麻烦

这不是真实的代码。真实的登录代码应包括更多内容,例如保护通道,加密等。 作为尝试一些概念的练习,没关系。

我看到您希望将所有内容保存到文件中,建议您首先尝试使用逻辑结构。 总是使打开的文件无法更新等混乱。 另一个建议是使不同的功能保持不同的功能,更易于维护和调试。

我看到字典,键(登录)和值(密码)的完美用法。 创建一个类并在主函数中放置options_menu()会很好。

dict_logs = {'afrank':'xyz123','lcroft':'xpto0007','happy_bird':'123abc'}

#check name and password are in a dictionary
def hello_login():

    isUser = False #start as False

    print('What is your user name?')
    user = str(input())

    for u,p in dict_logs.items():

        if u == user:
            dictpass  = p
            isUser    = True
    if isUser:
        print('Enter password')
        password = str(input())
        if dictpass == password:
            print('Login Success!')
        else:
            print('Error: wrong password')
            options_menu() 
    else:
        print('Error: wrong user name')
        options_menu()


user_info = {392111111:['afrank',2018],392111131:['lcroft',392113005:['happy_bird',2019]}         

# check if information is in a dictionary and updates another dict  
def reset_password():

    isUser  = False #start as False
    changed = False #start as False

    print('What is your student ID?')
    sid = int(input())

    for ui,data in user_info.items():

        if ui == sid:
            checksOn = data
            isUser = True

    if isUser:

        print('What is your user name?')
        uname   = str(input())  #accept parameter user_name

        if checksOn[0] == uname:
            print('What year were you admitted?')
            year = int(input())  #accept parameter year of admission

            if checksOn[1] == year:

                while not changed: #loop if changed not True
                    print('Enter a new password')   
                    newPass = str(input()) #accept parameter password

                    if dict_logs[uname] != newPass: #must be distinct of saved password             
                        print('Confirm password')
                        confPass = str(input())

                        if confPass == newPass:     #confirmation ok
                            dict_logs.update({uname:confPass})
                            changed = True
                            print('Password changed!')
                        else:
                            print('Error:passwords are distincts!')
                    else:
                        print('Error. Your password cannot be a previously used password.')

            else:
                print('Error. Year of addmission is wrong')
        else:
            print('Error. User name is wrong')
    else:
        print('Error! Please enter your Student ID Number')


# menu of options   
def options_menu():

    print('1. Login'+'\n'+'2. Reset Password'+'\n'+'3. Quit'+'\n'+'What would you like to do?')
    option = int(input())

    if option == 1:

        hello_login()

    elif option == 2:

        reset_password()

    elif option == 3:

        pass
,

好吧,这么快就免责声明此代码中的所有代码都是sudo代码,例如,它可以让您了解我的思路。

因此,我不会以下列方式看到您的问题。

  • 用户输入(操作)
  • 数据库处理

如果我对此没错,建议您采用以下方法。

  1. 通过编写一个类来处理特定用户在正在使用的数据库外以及进入字典或任何其他数据结构中的获取和设置,从而照顾数据库基本的csv等的代码,以后将使您的操作更清晰,更简单。像下面这样的东西可以解决问题。

    class UserDataBase(object):
    
        def __init__(self):
            self.database = db
    
        def get_user(self,username) -> dict:
            return {"Joe": {"password": "uber_secret"}}
    
        def set_user(self,username,user_obj: dict) -> None:
            db[username] = user_obj
    
  2. 接下来是时候照顾用户输入了,同样,它应该是一个对象,该对象将定义并执行您在用例中定义的操作。

    class Actions(object):
    
        def __init__(self,database):
            self.database = UserDataBase()
    
        def login(self,password) -> bool:
             if self.database.get_user(username) == password:
                    return True
             return False
    
        def reset_password(self,old_password,new_password,confirmed_password) -> bool:
            reset_code
    
        def quit(self):
            quit()
    

这将给您留下两个对象,您可以将它们绑在一起,以更简单地完成想要做的事情。

def get_input():

    actions = Actions(
        database=UserDataBase()
    )
    i = input(' 1. Login \n 2. Reset Password \n 3. Quit \n What would you like to do? \n ')

    if i == 1:
        actions.login(username=input('username: '),password=input('password: '))

get_input()

如果您对此方法有任何疑问,请告诉我。

本文链接:https://www.f2er.com/3082914.html

大家都在问