Reputation: 13
I am studying Python for a little while and trying to resolve following problem: Part of verification routine is given below. When the user enters the password, it is checked to ensure it is between 8 and 15 characters and then requested to verify the password by typing it again. Require complete task(a) CONDITION 1, CONDITION 2, and task (b) IF ... ELSE statement. Code and test the program.
password1 = input("Please enter a new password, between 8 and 15 characters: ")
match = False
while CONDITION 1
while CONDITION 2
password1 = input ("Password must be between 8 and 15 characters - please re-enter: ")
endwhile
password2 = input ("Please verify password: ")
if ....
print
else
endif
endwhile
I complete the task (b) but in task (a) I have a problem with while loop condition "while match:". When I leave variable match = False and enter the password with required amount of characters the loop terminates straight away and does not do verification with if .. else statement. If I write match = True and on first attempt enter incorrect number of characters (with correct number it terminates the loop again) program goes for verification anyway ignoring the number of characters. If the password was the same (example 111 and 111) it goes back to the loop and asks again for the password with required amount of characters. Then if I enter after the right amount of characters and password verification is the same it terminates the program like it suppose to be. I think something with the condition "match" in while loop is not right but can't figure out what it should be. Thanks
password1 = input("Please enter a new password, between 8 and 15 characters: ")
match = True
while len(password1) < 8 or len(password1) > 15:
password1 = input ("Password must be between 8 and 15 characters - please re-enter: ")
while match:
password2 = input ("Please verify password: ")
if password1 == password2:
print ("Password is valid")
break
else:
print ("Password is invalid")
Upvotes: 0
Views: 2296
Reputation: 92
I think like myself you are new to this. I find the following wrong with your code interpretation of the problem: Condition 1 and 2 are misplaced If statement is in the wrong loop. Your match is also not switched.
Check this out:
def password_verification():
password1 = input('Please enter a new password, between 8 to 15 characters: ')
match = False
while match is False:
while len(password1) < 8 or len(password1) > 15:
password1 = input('Password must be between 8 to 15 characters - please re-enter password: ')
password2 = input('Please verify password: ')
if password1 == password2:
print('Password is valid')
match = True
else:
print('Password is invalid')
return match
password_verification()
Upvotes: 1
Reputation: 1157
You are matching only when the first time wrong number of characters were entered. What will happen if in the first time, user enters between 8 to 15 characters? The first while loop will not execute, and hence nothing will execute. Condition1 (length between 8 and 15) must be checked outside the loop, and the second while loop must be kept separate.
pass1 = input('Enter a password : ')
while len(pass1) < 8 or len(pass1) > 15:
print('Incorrect, try again')
pass1 = input('Enter a password : ')
# Program reaches here only if pass1 is between 8 to 15 characters, otherwise first loop will be infinite
pass2 = input('Verify the password : ')
if pass1 == pass2:
print('Valid')
else:
while pass1 != pass2:
pass2 = input('Invalid. Re-enter the correct password : ')
The above code first takes password1
, checks if it is within the 8-15 characters range. If it is not, the first while loop keeps on executing. When a valid input is given, it takes password2
, and checks for equality. If it matches for the first time, no further while loop is needed. If it doesn't match, the while loop keeps on executing till equal passwords are not provided. Hope this helps!
Upvotes: 1