0

So i'm making a password keeper for fun(not intended to be used for anything other than teaching myself), and I want the user to be able to edit their username and password.

The way I had it set up is it asks you for a username and password, it checks your responses against a couple of very specific values(line 74), if the statement was true, it would allow access and if it isn't, it would deny access.

I then tried to set the statement "if(yourUser == yourNewUser and yourPass == yourNewPass):" it gives me a variable undefined error when I try to log in which I sort of expected.

So I tried defining the variables early on, setting each of them as 0 so if the password is unchanged, it would go to the regular bit, and if they have been changed or if they don't equal 0, it would go to a different function that does the same thing and it sort of worked, but then denied me access...

I don't know what to try next. If you need any clarification on anything that I said, just ask and I'll try to help.

Here's my code:

#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')
Ari Madian
  • 357
  • 1
  • 4
  • 13
  • I think you need `global yourNewUser, yourNewPass` in `changedPass`. Otherwise it assigns to local variables, not the global variables. – Barmar Apr 04 '16 at 23:47
  • what is the difference between a global and local variable and how do I define a variable as global? Also, I'm pretty new at python so try to use layman's if possible. Thanks! – Ari Madian Apr 04 '16 at 23:52

1 Answers1

0

The YourNewUser and YourNewPass variables are being initialized to 0 in the overall program. What the issue is, once you enter a function, those variables are not necessarily accessible by the function. When you set YourNewUser and YourNewPass in the function, it does not change the variables of the same name in the overall program. They are two different variables even though they have the same name.

One way to get around this is to have the function return the reset username and password in a list at the end and then set the username and password based on the index of the returned list. For example:

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

  • This is a bit confusing to me so I'm gonna ask a couple questions. – Ari Madian Apr 05 '16 at 01:27
  • So, you're saying that because the new user and new pass variables are set outside of the function, they aren't accessible inside the function? Also, someone above mentioned global variables, would changing all the instances of the myNewUser and myNewPass variables to global variables fix the "same name, different variable" issue? I understood the first part of your explanation, but no quite the second part, could you please try to explain it differently? Thanks for a more detailed answer! – Ari Madian Apr 05 '16 at 01:39