I am trying to learn how to tell if my code will be better off creating a class as opposed to using many functions. I am a new Python programmer trying to get the hang of when classes should be implemented.
Determines whether the username is valid or invalid:
def is_good_user(user):
is_user_good = True
if user in database:
print "Sorry! Username is taken! Create another."
is_user_good = False
if not 5 < len(user) < 25:
print "Username must be at least 6 characters!"
is_user_good = False
if is_ascii(some_string="") is not True:
print "Please create username that follows ASCII formatting."
is_user_good = False
return is_user_good
Determines whether the password is valid or invalid:
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 set(database).intersection(password):
print "Password Must Contain Alphanumeric Characters!"
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
return is_password_good
These two functions are amalgamated into one function to create a username and password:
def create_user(database):
while True:
user = raw_input("Enter a New Username: ")
if is_good_user(user):
print "Great Job! Username Successfully Created! Time to Create a Password!"
break
print "That's not a good username, please try again!"
while True:
passcode = raw_input("Enter a New Password: ")
if is_good_password(passcode):
print "Welcome! Username & Password successfully created!"
break
print "That's not a good password, please try again!"
database[user] = passcode
make_digest(password=passcode, salt=None)
dump_data()
Should I add in a class for readability of my code? Being new at Python, I'd like to take up the challenge if adding a class is necessary.
database
appears only as a local increate_user
, but not as a global or other nonlocal for theis_good_*
functions. – Michael Urman Mar 2 at 14:58