In: Computer Science
In PYTHON:
Write a program that asks for a password from a user and checks if it satisfies the conditions:
If the password is valid, then the program prints “password accepted”. Otherwise, it keeps asking for more passwords from the user until a valid password is obtained.
Hint: You might like to check the string methods that start with the letter ‘i’.
# do comment if any problem arises
# Code
# this method returns true if given password has atleast one lowercase letter
def has_lower(password):
for char in password:
if char.islower():
return True
return False
# this method returns true if given password has atleast one uppercase letter
def has_upper(password):
for char in password:
if char.isupper():
return True
return False
# this method returns true if given password has atleast one digit
def has_digit(password):
for char in password:
if char.isnumeric():
return True
return False
# this method returns true if given password is valid
def isValid(password):
# if length of password is less than 6
if len(password) < 6:
return False
# if password contains spaces
if ' ' in password:
return False
# if password doesnot has lowercase letter
if not has_lower(password):
return False
# if password doesnot has uppercase letter
if not has_upper(password):
return False
# if password doesnot has digit
if not has_digit(password):
return False
# if all above conditions are satisfied then password is valid
return True
def main():
password = input("Enter a password: ")
# while password is not valid
while not isValid(password):
print("Invalid password!\n")
# reprompt for password
password = input("Enter a password: ")
print("Password accepted")
main()
Screenshot of code:
Output: