如何编辑变量的值供以后使用?

How to edit a variable's value for later use?

所以我制作一个密码管理器只是为了好玩(除了自学外不打算用于任何其他用途),我希望用户能够编辑他们的用户名和密码。

我设置它的方式是它要求你输入用户名和密码,它会根据几个非常具体的值检查你的回答(第 74 行),如果声明为真,它将允许访问,如果它不是,它会拒绝访问。

然后我尝试设置语句 "if(yourUser == yourNewUser and yourPass == yourNewPass):" 当我尝试登录时它给我一个可变的未定义错误,这是我所期望的。

所以我尝试尽早定义变量,将每个变量都设置为 0,这样如果密码未更改,它将转到常规位,如果它们已更改或不等于 0,它会转到一个不同的功能,做同样的事情,它有点工作,但后来拒绝我访问...

我不知道接下来要尝试什么。如果您需要对我所说的任何内容进行任何澄清,请尽管提问,我会尽力提供帮助。

这是我的代码:

#The goodbye or end statement
def goodbye():
    print('Good Bye! :{D')
#The main password request statement
def request():
    reqPass = input('Which password would you like?[Google, Twitter, Reddit, Computer]')
    #still need to figure out dictionary manipulation so this is a temporary sytem.
    if(reqPass == 'Google' or reqPass == 'google'):
        print('________________')
        print('Pass: GOOGLEPASSHERE')
        print('________________')
        reqSecPass = input('Request another password?[y/n]')
        if(reqSecPass == 'y' or reqSecPass == 'Y'):
            request()
        else:
            goodbye()
    elif(reqPass == 'twitter' or reqPass == 'Twitter'):
        print('_________________')
        print('User: TWITTERUSERHERE')
        print('Pass: TWITTERPASSHERE')
        print('________________')
        reqSecPass = input('Request another password?[y/n]')
        if(reqSecPass == 'y' or reqSecPass == 'Y'):
            request()
        else:
            goodbye()
    elif(reqPass == 'computer' or reqPass == 'Computer'):
        print('________________')
        print('Pass: COMPUTERPASSHERE')
        print('________________')
        reqSecPass = input('Request another password?[y/n]')
        if(reqSecPass == 'y' or reqSecPass == 'Y'):
            request()
        else:
            goodbye()
    elif(reqPass == 'reddit' or reqPass == 'Reddit'):
        print('_________________________')
        print('User: REDDITUSERHERE')
        print('Pass: REDDITPASSHERE')       
        print('________________')
        reqSecPass = input('Request another password?[y/n]')
        if(reqSecPass == 'y' or reqSecPass == 'Y'):
            request()
        else:
            goodbye()
    #This is the start of the changed password function     
def changedpass():
    if(yourUser == yourNewUser and yourPass == yourNewPass):
        dirCheck = input('Account settings?[y,n]')
        if(dirCheck == 'y' or dirCheck == 'Y'):
            #This is the start of the password/username thing
            print('this function is not working yet!')
            actSetCheck = input('Change username or password?')
            if(actSetCheck == 'user' or actSetCheck == 'User' or actSetCheck == 'Username' or actSetCheck == 'username'):
                yourNewUser = input('What would you like your new username to be?')
            elif(actSetCheck == 'pass' or actSetCheck == 'Pass' or actSetCheck == 'password' or actSetCheck == 'Password'):
                yourNewPass = input('What would you like your new password to be?')
        elif(dirCheck == 'n' or dirCheck == 'N'):
            request()

#setting the variables early on
yourNewUser == 0
yourNewPass == 0
#This is the "title screen"    
print('_____This is a password keeper_____')
#checking if the user has an account
actCheck = input('Do you already have an account?')
if(actCheck == 'Yes' or actCheck == 'yes'):
    #Checking to see if the yourNewUser or yourNewPass variable has been changed
    if(yourNewUser != '0' or yourNewPass != '0'):
        changedpass()
    #asking for user's name and password
    else:
        yourUser = input('___What is your Username?___')
        yourPass = input('___What is your Password?___')
        if(yourUser == 'ari' and yourPass == 'rycbar1234'):
            dirCheck = input('Account settings?[y,n]')
            if(dirCheck == 'y' or dirCheck == 'Y'):
                #This is the start of the change password/username thing
                print('this function is not working yet!')
                actSetCheck = input('Change username or password?')
                if(actSetCheck == 'user' or actSetCheck == 'User' or actSetCheck == 'Username' or actSetCheck == 'username'):
                    yourNewUser = input('What would you like your new username to be?')
                elif(actSetCheck == 'pass' or actSetCheck == 'Pass' or actSetCheck == 'password' or actSetCheck == 'Password'):
                    yourNewPass = input('What would you like your new password to be?')
            elif(dirCheck == 'n' or dirCheck == 'N'):
                request()
        #incorrect password thing
        else:
            print('Incorrect Username or password')

YourNewUser 和 YourNewPass 变量在整个程序中被初始化为 0。问题是,一旦你进入一个函数,这些变量不一定能被函数访问。当您在函数中设置 YourNewUser 和 YourNewPass 时,它不会改变整个程序中的同名变量。尽管它们具有相同的名称,但它们是两个不同的变量。

解决此问题的一种方法是在末尾的列表中使用 return 重置用户名和密码的功能,然后根据 return 的索引设置用户名和密码编辑列表。例如:

NewPass = ChangedPass() YourNewUser = NewPass[0] yourNewPass = NewPass[1]