Python / startswith & endswith

def input(password):

while password[0] == '#' or password[-1] == '#':
    print("Your password contains '#' in either the first or last character, please try again.")
    password = str(input("Please create a password:\n"))

    

return True

print(input(password))

Why does this code not ask the user to reenter a new password so it can text the condition of the while loop again and instead just returns True?

There’s a bit of missing information:

How are you invoking this function, and with what argument? If invoked correctly it should work.

Also, it’s not good to name the function input since it’s a reserved word (you even use input method within this function… which would make this… recursive? But I think this is unintentional).

Example (this would work):

def inp(password):
    while password[0] == '#' or password[-1] == '#':
        print("Your password contains '#' in either the first or last character, please try again.")
        password = str(input("Please create a password:\n"))
    return True

inp("#test")

That worked, in the code above I initially prompt the user for an input. I haven’t gotten that far into the program where I have invoked the functions. I’ve just been testing them one at a time and hit a wall with this one.

Thank You!

My follow up question would be:

So I have multiple functions that return True if they fit the criteria of the conditional statement within the function. How could I write a while loop /if statement that would test what each function returns and if it returns false to rerun all of the functions again.

Honestly you’d just have to hunker down and try it. I find it really helps to map it out on paper (using just words/pseudocode). The first step is just to get it to work. Afterwards you could think about how to re-factor it so that it’s more efficiently written.

Sounds. good, I’ll try a bunch of different things.

Thank you!

1 Like