Prompts the user for a password. Check if the password is valid where a valid password has the following criteria. It must be at least 5 characters long. It must start with either a c or a 5. It must contain at least one special character from &,@,# and %
def CheckPassword(password):
if len(password) < 5:
raise ValueError('The password must be at least 5 characters long')
if password[0] != 'c' and password[0] != '5':
raise ValueError('The password must start with either a c or a 5')
for ch in ['&', '@', '#', '%']:
if ch in password:
return
raise ValueError('The password must contain at least one special character from &,@,# and %')
try:
password = input('Input password: ')
CheckPassword(password)
print('The password is valid')
except ValueError as e:
print('Error:', e)
Comments
Leave a comment