"Return out side of function" error


So I’m working on a simple password project that takes the user’s input and if it’s equal to the password it grants access. This is my code:

password = "testPASSWORD"
accessGranted = false
encrypted = "etc etc etc yay it works"
attempt = raw_input("Please enter your password")

if attempt != password:
    print "Incorrect password"

if attempt == password:
    print "Access granted. Printing information."

if attempt == password:
    return accessGranted == true

if accessGranted == true:
    print encrypted

When I run it, I get the following error:

file "password.py", line 16
      return accessGranted == true
Syntax Error: 'return' outside of function

Any help would be appreciated, I’m quite new to Python


the error is quite clear? return can be used outside of a function


Yes, I know what the error is, but my return is inside an if function


The statement where you are allowed to use return is def, not if
if is not a function nor does it create one
I’m not sure what you think return was supposed to do there, or how you’d argue for that return would do that


Ah, okay well thanks :smiley:


You can shorten your logic quite a bit

if attempt == password:
    print "Access granted. Printing information."
    print encrypted
    print "Incorrect Password"