0

I'm trying to make a password with typical requirements like it has at least 1 uppercase/lowercase, etc. If the password is not valid according to the requirements, we have to display the errors in order for the user to try to get it correct again.

I started out with a while loop so that in the end the user will have an option to continue with another test or not. These are general steps I did.

At the end, if the user's text input is determined to not be valid, I have to display what his/her errors were. That's my main problem now. The code is better after a suggestion. Now I just have to display the errors somehow.

Here's how my code went

while True:
   pw = input('Enter password to be tested if valid or not: ')
   correct_length = False
   uc_letter = False
   lc_letter = False
   digit = 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
   else:
      valid_pw = False
      #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

2 Answers2

3

isdigit only returns True or False.

if ch.isdigit():

If you want to check if the first two characters are digits, do it outside the loop:

if pw[:2].isdigit():
    digit = True
for ch in pw:
    ...

And to check if there are spaces in the string:

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

Or if you want to escape all kinds of white spaces and blanks, including newline characters:

import string
...
if not any(c in string.whitespace for c in pw):
    no_blanks = True
for ch in pw:
   ...
aIKid
  • 26,968
  • 4
  • 39
  • 65
  • Thank you! I used your suggestion to adjust my code. It's working much better. I'm still having problems trying to display what is the error for the user when the password has been determined in valid towards the end right before the question asks the user to continue. – user3057499 Dec 02 '13 at 14:25
  • Sorry, wrong link. Fixed, the accept button is in the left side of the question :) – aIKid Dec 02 '13 at 14:30
  • Umm.. i think that's a separate question from this. Post a new question, and show exactly what do you mean by "display user's errors" – aIKid Dec 02 '13 at 14:39
1

For the white space I would use (don't forget to import string):

import string

for ws in string.whitespace:
    if ws in pw:
        no_blanks = False
        break

This checks for all kinds of white space, including for example Space and Tab

For the digits I would define dig_count = 0 before your for-loop.

Inside the for-loop:

if ch.isdigit():
    dig_count += 1

After the for-loop:

if dig_count >= 2:
    digit = True
aIKid
  • 26,968
  • 4
  • 39
  • 65
CML
  • 353
  • 7
  • 18