Question

I want this function to work in my is_password_good function.

def is_ascii(some_string) :
    for each_letter in some_string:
        if ord(each_letter) < 128:
        return False
    return True

The is_good_password function makes certain the user's password is at least 10 characters long and that at least one uppercase and lowercase exists.

How can I pass my ASCII function to check if the user creates a passwords using at least one symbol by ASCII standards?

def is_good_password(password):
    count_upper, count_lower = 0, 0
    for characters in password:
        if characters.isupper():
        count_upper += 1
        if characters.islower():
        count_lower += 1
    is_password_good = True
    if len(password) <= 10:
        print "Password is too weak, must be more than 10 characters long!"
    is_password_good = False
    if count_upper < 1 or count_lower < 1:
        print "Password must contain at least one uppercase and one lowercase character!"
        is_password_good = False
        create_user(database)
    print "Welcome! Username & Password successfully created!"
return is_password_good

No correct solution

OTHER TIPS

You can check string.punctuation exist in string or not.

>>>string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
import re

def getmix(password):
    Upper=len(set(re.findall(r'[A-Z]',password)))
    Lower=len(set(re.findall(r'[a-z]',password)))
    Nums=len(set(re.findall(r'[0-9]',password)))
    Symb=len(set(re.findall(r'[~!@#$%^&\*()_+=-`]')))
    return (Upper, Lower, Nums, Symb)

Should give you a good starting point.

The function somestring.isalnum() will return False if not all characters in the string are alphabetics or numbers.

The precise definition of these categories are locale-dependent; make sure you know which locale you are using.

By the by, ASCII is only defined up to character code 127. If you go above 127, you need to know which character set and encoding you are dealing with. However, characters like # and ! are indeed defined in ASCII, and have character codes in the 30-something range. You are better off using library functions which abstract away the precise character codes, anyway.

has_symbol = False
for c in '~!@#$%^&*()_+=-`':
    if c in password:
        has_symbol = True
        break
if not has_symbol:
    print "Password must contain at least one uppercase and one lowercase character!"
    is_password_good = False

Always use the builtins, don't roll your own, so in that spirit, use the string module for a canonical list of symbols:

import string

symbols = string.punctuation

and printing symbols shows us these characters:

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

You can pass to any the "one iterable in another" construction:

if any(char in symbols for char in some_string):
    print 'password good'

However, I actually prefer the set methods instead of the above construction:

if set(symbols).intersection(some_string):
    print 'password good'

but Triplee's advice on isalnum is just as potent, doesn't require importing the string module, and quite shorter.

if not some_string.isalnum():
    print 'password good'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top