Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display my user's errors?

My user wants to make a password and I'm supposed to be checking if it's valid or not. So far, I have down the code to check if it is valid/not valid. Now, the next step (after determining it is not valid) is to tell the user it is not valid AND why their password is not a valid option.

while True:
   pw = input('Enter password to be tested if valid or not: ')
   correct_length = False
   uc_letter = False
   lc_letter = False
   no_blanks = True
   first_letter = False

   if len(pw) >= 8:
   correct_length = True

   for ch in pw:
      if ch.isupper():
         uc_letter = True

      if ch.islower():
         lc_letter = True

   if pw.isalnum():
      digit = True

   if pw[:1].isalpha():
      first_letter = True

   if not pw.find(' '):
      no_blanks = True

   if correct_length and uc_letter and lc_letter and digit and first_letter and no_blanks:
      valid_pw = True
      print('Your password to be tested is valid.')
   else:
      valid_pw = False
      print('Your password to be tested is not valid because:')
      print(----------)
      #This is the part where I'm suppose to display the errors if the user gets it wrong.
      #Initially, in the test for ch. above, I put in an else: with a print statement but because of the for- statement, it prints it out for every single character.

   answer = input('Try another password input? y/n ')
   if answer == 'y':
      answer = True
   else:
      break
like image 557
user3057499 Avatar asked Mar 27 '26 05:03

user3057499


1 Answers

Hm.. I think you can simply put the extra else statement, then raise an error:

   if not pw.find(' '):
      no_blanks = True
   else:
      raise ValueError('Invalid input!')

And similarly with your other conditionals.

If you want your loop to keep going, you can just print the message, and then continue:

else:
    print("Invalid input! Please re enter it:")
    continue

Hope this helps!

like image 71
aIKid Avatar answered Mar 28 '26 18:03

aIKid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!