2

So, I've been coding for a Python 'computer software', one where you basically have a log-in username and password that can be edited. I tried to add a feature that allows you to 'remove' the password, (but is more like skipping it) but it won't work. Here is the code for the skipping procedure:

def Access1(): 
    print("Input first name")
    print("")
    answer = raw_input()
    if(answer.lower() == username and PasswordSkip == True):
        print("")
        print("Access granted")
        List1()
    if(answer.lower() == username and PasswordSkip == False):
        print("")
        print("Access granted")
        Access2()

*Note that Access2() is the function where a password is needed to continue, and List1() is the main menu of the system.

Here is where the boolean is set to True:

def PasswordRemove():
    print("")
    print("Are you sure you want to remove your password, <yes> or <no>?")
    print("")
    key = raw_input()
    if(key.lower() == "yes"):
        PasswordSkip = True
        print("")
        print("Ok, your password has been removed.")
        print("")
        List1()
    elif(key.lower() == "no"):
        DetailsChange()
    else:
        DetailsChange()

And here is how I define and globalise PasswordSkip:

PasswordSkip = False

def Startup():
    global PasswordSkip

(the function goes on longer, but has no involvement in the boolean.)

If you need any more infor ation on my code, I'm able to give it to you.

So, when I run my code, it overlooks the if statement about the boolean. If the boolean is True, it should go to a different function. However, it skips the statement because it brings up Access2() (the password function).

Answers aren't urgently needed, but thanks.

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
oisinvg
  • 592
  • 2
  • 8
  • 21
  • 2
    `global` statements set the scope of a name *per function*. `global PasswordSkip` in `Startup` won't alter the scope of the name in a different function. – Martijn Pieters Oct 05 '15 at 21:16
  • btw, are you sure you want to grant access in either case `PasswordSkip == True` and `PasswordSkip == False`? – Pynchia Oct 05 '15 at 21:19

1 Answers1

3

The fragment

def Startup():
    global PasswordSkip

does not declare PasswordSkip to be global everywhere. It declares that within this function, references to setting PasswordSkip should be taken to be the global variable PasswordSkip rather than some local variable.

Now in the function PasswordRemove, you do not declare that PasswordSkip is a global to be brought into scope. Thus statements like PasswordSkip = True are setting a local variable PasswordSkip

donkopotamus
  • 22,114
  • 2
  • 48
  • 60