"Return out side of function" error


#1

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


#2

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


#3

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


#4

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


#5

Ah, okay well thanks :smiley:


#6

You can shorten your logic quite a bit

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