-1

I have a task for my college (I am beginner), which asks you to validate a password using ASCII characters. I tried using simple code and it worked, however it kept skipping my ASCII part. Requirement list:

1.4 Call function to get a valid password OUT: password
1.4.1 Loop until password is valid 1.4.2 Ask the user to enter a password 1.4.3 Check that the first character is a capital letter (ASCII values 65 to 90) 1.4.4 Check that the last character is #, $ or % (ASCII values 35 to 37) 1.4.5 Return a valid password

U = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
upCase = ''.join(chr(i) for i in U)
print(upCase) #Ensure it is working

def passVal(userPass):
    SpecialSym = ["#", "$", "%"]
    val = True

#Common way to validate password VVV
    if len(userPass) < 8:
        print("Length of password should be at least 8")
        val = False
    if not any(char.isdigit() for char in userPass):
        print("Password should have at least one numeral")
        val = False

#I Tried same with ASCII (and other methods too) but it seemed to be skipping this part VVV
    if not any(upCase for char in userPass):
        print("Password should have at least one uppercase letter")
        val = False


    if not any(char.islower() for char in userPass):
        print("Password should have at least one lowercase letter")
        val = False
    if not any(char in SpecialSym for char in userPass):
        print("Password should have at least on fo the symbols $%#")
        val = False
    if val:
        return val


def password():
    if (passVal(userPass)):
        print("Password is valid")
    else:
        print("Invalid Password !!")

userPass = input("Pass: ")
password()

1 Answers1

0

From Python 3.7 you can use str.isascii()...

>>> word = 'asciiString'
>>> word.isascii()
True

Otherwise you could use:

>>> all([ord(c) < 128 for c in word])
True

Since all ASCII characters have an ordinal (ord) value less than 128 (0 -> 127): https://en.wikipedia.org/wiki/ASCII

So your logic will either be (3.7+):

if word.isascii():
    # string is ascii
...

Or:

if all([ord(c) < 128 for c in word]):
    # string is ascii
else:
    # string contains at least one non-ascii character
Adam
  • 709
  • 4
  • 16
  • Much appreciated, will try! – Vladislavs Mar 27 '20 at 11:39
  • What if I need to use a specific range of letters from ASCII. They are 65-90 (all uppercase letters). I am completely new to this so I am sorry for inconvenience. – Vladislavs Mar 27 '20 at 12:02
  • So using `ord`, you check if a single character is in `range(65, 90+1)` which would give the same output as `.isupper()` - but there's no reason why you should be using `ord` instead of the built-in methods like `.isupper()` if you can. – Adam Mar 27 '20 at 12:09
  • You could also try using `re` like the answer from JvdV if you were looking for something more complex. – Adam Mar 27 '20 at 12:11
  • I left a comment to a post of other helper, could you please check it. I also assume range(65, 90+1) is just what I need. Going to try it out now. – Vladislavs Mar 27 '20 at 12:16
  • Everything worked, big thanks to all of you. I'm sorry that there were lack of information from my side and things have gotten a little bit mess. – Vladislavs Mar 27 '20 at 12:31
  • No worries - if it's helped you please consider accepting it as the answer! :) – Adam Mar 27 '20 at 12:55