0

I have created a variable inside the Start() procedure but it doesn't seem remember the variable outside of the procedure. Is there a way of getting python to remember the variable outside of the Start() procedure? My code is as follows:

def Start():
    password = raw_input("Enter A Password With 7 Or More Characters: ")
    passwordLength = len(password)
    if passwordLength<7:
        print "Your entered a password with less than 7 characters. Enter a longer password."
        Start()
    else:
        reEnter = raw_input("Re-enter your password: ")
        if reEnter<>password:
        print "Your passwords did not match, please try again."
        Start()
        if reEnter==password:
        print "Your password has been saved."


Start()

print password
print passwordLength
Lafexlos
  • 7,618
  • 5
  • 38
  • 53
Alderp
  • 23
  • 2

3 Answers3

1

You can return your variable at the end of your function.

def Start():
    password = input("Enter A Password With 7 Or More Characters: ")
    passwordLength = len(password)
    if passwordLength<7:
        print("Your entered a password with less than 7 characters. Enter a longer password.")
        return Start()
    else:
        reEnter = input("Re-enter your password: ")
        if not reEnter == password:
            print("Your passwords did not match, please try again.")
            return Start()
        else:
            print("Your password has been saved.")
            return password, passwordLength

savedPass, passLength = Start()

print(savedPass)
print(passLength)

EDIT: Edited style (thanks @jrennie) and recursive calls. (thanks @bgporter)

Lafexlos
  • 7,618
  • 5
  • 38
  • 53
  • Style nits: If you use underscores to separate words, please don't capitalize. Also, if you are treating `print` like a function (3.x style), there shouldn't be a space between `print` and the left paren. – jrennie Mar 15 '14 at 20:10
  • thanks for the heads up. Changed variables to mixed case since it is much suitable to OPs style. – Lafexlos Mar 15 '14 at 20:13
  • In the recursive calls to `Start()` in both the `if` and `else` branches, you need to return the tuple returned from those calls for things to work correctly -- `return Start()` – bgporter Mar 15 '14 at 20:16
1

I am assuming here that password and passwordLength are global variables. For this, you would use the global keyword, by simply at the beginning of your Start() defining them like so:

def Start():
    global password
    global passwordLength

    password = raw_input("Enter A Password With 7 Or More Characters: ")
    passwordLength = len(password)
    if passwordLength<7:
        print "Your entered a password with less than 7 characters. Enter a longer password."
        Start()
    else:
        reEnter = raw_input("Re-enter your password: ")
        if reEnter<>password:
            print "Your passwords did not match, please try again."
            Start()
        if reEnter==password:
            print "Your password has been saved."

However, globals are often frowned upon in python. If possible try and use return to print your output, but for most cases global is just fine.

theage
  • 289
  • 1
  • 3
  • 10
0

Do

def Start():
    global password
    global passwordLength
    ....
schacki
  • 9,401
  • 5
  • 29
  • 32