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

时间:2016-04-04 23:37:07

标签: python variables input save

所以我正在设置密码管理员以获得乐趣(不打算用于除自学之外的其他任何事情),我希望用户能够编辑他们的用户名和密码。

我设置它的方式是它要求你输入用户名和密码,它会根据一些非常具体的值检查你的响应(第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')

1 个答案:

答案 0 :(得分:0)

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

解决此问题的一种方法是让函数在末尾的列表中返回重置用户名和密码,然后根据返回列表的索引设置用户名和密码。例如:

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

相关问题