this is my current code:
while True:
try:
username = input("Username: ")
if len(username) < 8:
print ("Sorry, the username must be at least 8 characters long.")
if username.isalnum() == False:
print ("Sorry, your name can only contain alpha numeric characters")
numupper = 0
for c in username:
if c.isupper() == True:
numupper += 1
if numupper > 0:
print ("You have at least 1 uppercase in this username.")
else:
print ("You have no uppercase in this username.")
numlower = 0
for d in username:
if d.islower() == True:
numlower +=1
if numlower > 0:
print ("You have at least 1 lowercase in this username.")
else:
print ("You have no lowercase in this username.")
numdigit = 0
for e in username:
if e.isdigit() == True:
numdigit += 1
if numdigit > 0:
print ("You have at least one digit in this username.")
else:
print("You have no digits in this username.")
else:
print("Please try again")
continue
except:
print ("Sorry, not valid. Try again.")
else:
print ("Thank you for your input")
break
when I run this definition using my main program:
import uservalidation
username = input("Username: ")
result, reason = uservalidation.valid_username(username)
if not(result):
print (reason)
I get this (depending on how many letters i enter in):
Username: craig
Sorry, the username must be at least 8 characters long.
You have no uppercase in this username.
You have no uppercase in this username.
You have no uppercase in this username.
You have no uppercase in this username.
You have no uppercase in this username.
You have at least 1 lowercase in this username.
You have at least 1 lowercase in this username.
You have at least 1 lowercase in this username.
You have at least 1 lowercase in this username.
You have at least 1 lowercase in this username.
You have no digits in this username.
You have no digits in this username.
You have no digits in this username.
You have no digits in this username.
You have no digits in this username.
Please try again
How can I change my code so that it’ll only show statements such as “you have at least lowercase in this username” only once. thanks you so much
Your indentation is off:
So for each character, you are printing the error text. What you want to do instead is to move the check on the number outside of the loop:
So after you have counted the upper case characters (in the loop), you evaluate the results and print the error text once.
The same applies to the lowercase- and digit-check.
Btw. you can use the same variable for different for-loops. So you do not need to use
c,dande, you can just keep usingc. Also, you should consider merging your loops. In all three of them, you loop over the characters of the username, so you can do everything at the same time:Also, as you can see, I removed the
== Truefrom the if conditions. It is generally a good practice to leave that out, asifalready checks if the expression equals to true. So it’s basically redundant.And finally, you can do those checks a bit differently. For example you can check for lower- and uppercase characters by converting the string to the same and compare it with the original text. So
username.lower() == usernamemeans that there are no uppercase characters in it; similarilyusername.upper() == usernamemeans that there are no lowercase characters.