139k views
4 votes
Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has between 8 and 15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests. The function enterNewPasswordshould return the new password.

User Jf Beaulac
by
5.2k points

1 Answer

2 votes

Answer:

def enterNewPassword():

while True:

password = input("Enter password: ")

has_digit = False

for i in password:

if i.isdigit():

has_digit = True

break

if not has_digit or (len(password) < 8 or len(password) > 15):

if len(password) < 8 or len(password) > 15:

print("The password length must be between 8 and 15!")

if not has_digit:

print("The password must include at least one digit!")

else:

return password

print(enterNewPassword())

Step-by-step explanation:

*The code is in Python.

Create a function named enterNewPassword that takes no parameter

Create an indefinite while loop. Inside the loop, ask the user to enter the password. Initialize the has_digit as False, this will be used to check if password contains a digit or not. Create a for loop, that that iterates through the password, if one character is digit, set the has_digit as True and stop the loop (Use isdigit() to check if the character is digit or not).

After the for loop, check if has_digit is false or the length of the password is not between 8 and 15. If the length of the password is not between 8 and 15, print the error. If has_digit is false, again print the error.

Otherwise, password met the requirements and return the password

Call the enterNewPassword() function and print the result

User Youwhut
by
5.1k points