Reputation: 43
I've asked a few questions about this code and luckily I'm almost finished but I have one final problem.
These are my functions: def valid_username(username): if not username.isalnum(): reason = ('username can only contain alpha numeric numbers') return '', reason
numupper =0
for c in username:
if c.isupper():
numupper = numupper + 1
if numupper <= 0:
reason=('username must contain at least one uppercase character')
return '',reason
numlower =0
for c in username:
if c.islower():
numlower = numlower + 1
if numlower <= 0:
reason=('username must contain at least one lowercase character')
return '', reason
if len(username)<8:
reason = ('username must be greater than 8 characters')
return '',reason
numdigit=0
for c in username:
if c.isdigit():
numdigit = numdigit + 1
if numdigit <= 0:
reason= ('username must contain at least one number')
return '',reason
else:
return username, ''
def valid_password(password): if not password.isalnum(): pwreason = ('password can only contain alpha numeric numbers') return '', pwreason
numupper =0
for c in password:
if c.isupper():
numupper = numupper + 1
if numupper <= 0:
pwreason=('password must contain at least one uppercase character')
return '',pwreason
numlower =0
for c in password:
if c.islower():
numlower = numlower + 1
if numlower <= 0:
pwreason=('password must contain at least one lowercase character')
return '', pwreason
if len(password)<8:
pwreason = ('password must be greater than 8 characters')
return '',pwreason
numdigit=0
for c in password:
if c.isdigit():
numdigit = numdigit + 1
if numdigit <= 0:
pwreason= ('password must contain at least one number')
return '',pwreason
if username in password:
print('username cannot be used as part of your password')
else:
return password, ''
The username one works perfectly and the password one does too to an extent. This is my program:
import uservalidation
# get a username from the user
username = input("Username: ")
# validate username
result, reason = uservalidation.valid_username(username)
# if it isn't valid we should tell them why
if not(result):
print (reason)
# otherwise the username is good - ask them for a password
# get a password from the user
password = input("Password: ")
# determine if the password is valid
pwresult, pwreason = uservalidation.valid_password(password)
# if the password isn't valid we should tell them why
if not(pwresult):
print (pwreason)
else:
print ("Username and Password combination is valid!")
The only part that doesn't seem to work is when I check to see if the password contains the username, the error I'm receiving states that username isn't a global variable but I thought it was since I ask the user to input it in the program outside of both functions. Thanks!
Upvotes: 0
Views: 8355
Reputation: 9460
You seem to have gotten confused with the try and except clause. What you're probably aiming for is something like this, which avoids using exceptions altogether.
def valid_username(username):
if not username.isalpha():
reason = 'Username can only contain alpha characters'
return reason
if not any(x.isupper() for x in username):
reason = 'Username must contain at least one uppercase character'
return reason
return "Username is good!"
print valid_username('FrankWorks')
print valid_username('frankfails')
Or using exceptions you could do:
def valid_username_using_exceptions(foo):
if not foo.isalpha():
raise ValueError('Username can only contain alpha characters')
return True
#.. Other validations functions... etc
failing_username = 'this_will_raise_an_exception_***'
try:
if valid_username_using_exceptions(failing_username):
print 'All good'
except ValueError, e:
print "Failed!", e
Upvotes: 0
Reputation: 3009
except
is used in the try...except
statement. You haven't added a try
clause to your code. It would seem from your code that you don't really understand how to handle exceptions properly.
This would be a good introduction for you.
Upvotes: 3