In: Computer Science
Python Problem 3
Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
def enterNewPassword():
while True:
# get user input
password = input('Enter password : ')
# if the length is not in range 8 - 15
if len(password) < 8 or len(password) > 15:
print('Error! Password is not in range 8-15')
digit_count = 0
arr = [ '1' , '2', '3', '4', '5', '6', '7', '8', '9', '0' ]
# traverse the string and count the number of digits
for ch in password:
# if current character is digit
if ch in arr:
digit_count += 1
# if there is not digit
if digit_count < 1:
print('Error! There must be atleast 1 digit')
# if password is correct
if len(password) >= 8 and len(password) <= 15 and digit_count >= 1:
print('Correct password')
return
print()
enterNewPassword()
Sample Output