def get_password():
password = passwd() #returns None when no input is given else return password
if password is not None:
if len(password)<7:
print("Minimum length of password is 6. Please try again.")
password = None
elif len(password)>16:
print("Maximum length of password is 16. Please try again.")
password = None
while password is None:
get_password()
return password
I want to run this function again and again until users inputs a valid password. If user gives a valid password then while loop should not be entered but even then the loop is getting executed endlessly.
>Solution :
Don’t use recursivity to have a logic of "do that again", because when you need to stop it, recursivity is for specific problems
Use a main loop based on password is None, then call passwd and apply your rules
def get_password():
password = None
while password is None:
password = passwd() # returns None when no input is given else return password
if password is not None:
if len(password) < 7:
print("Minimum length of password is 6. Please try again.")
password = None
elif len(password) > 16:
print("Too Long", "Maximum length of password is 16. Please try again.")
password = None
return password