#πŸ”’ Question about calling function vs asking for new input

26 messages Β· Page 1 of 1 (latest)

vernal sparrow
#

Hi there. So from what I know, strings are immutable, right?

So why then, does my program, return an old value. Say I type in one, then type in 1, it continues to return one, even if it's provided with a legitimate value.

The commented out line works, I just want to know why, when I call my function again, the variable "length" remains the same?

thanks

    #Implementing error handling for password generator.
    #Using a while loop to ensure that the correct value is entered. 
    length = input("Enter a length for the password (below 20 characters) \n")
    is_valid = False
    while not is_valid:
        #Check if length can be converted to an integer.
        #if it can be, then set is_valid to True and
        #end the loop.
        try:
            int(length)
            is_valid = True
        #If not an integer, print message not a number and req input again.
        #Ask for input again.
        except ValueError:
            print("That's not a number \n")
            print(length)
            get_password_length()
            #length = input("Enter a length for the password (below 20 characters \n")
    #Returns the value of length as an integer.
    return int(length)```
toxic craneBOT
#

@vernal sparrow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

#

Hey @vernal sparrow!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
cursive yew
#

so once the function resolves, it still ends up calling the function again and again for each failed attempt

vernal sparrow
cursive yew
#

this really wouldn't be the idea way to handle this situation

#

I would ask for input inside the loop

vernal sparrow
#

I understand, the commented out line works - which is the new implementation.

cursive yew
#

otherwise there isn't really a reason to have a loop AND recursive call

vernal sparrow
#

I just wanted to know the why behind it

cursive yew
#
def get_password_length():
    while True:
        length = input("Enter a length for the password (below 20 characters) \n")
        return int(length)
    except:
        print("That's not a number \n")
vernal sparrow
remote flame
vernal sparrow
cursive yew
#

it should never return its original value since the loop cannot be broken

vernal sparrow
#

okay.

cursive yew
#

you'll be stuck infinitely inside the except block

vernal sparrow
#

not from what I've seen here...

#

wait a sec

vernal sparrow
cursive yew
#

since you never ask for input again during that recursive depth, there's no escape

cursive yew
vernal sparrow
#

great, thanks!

toxic craneBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.