user1762229
user1762229

Reputation: 71

multiple outputs want to limit

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

Upvotes: 0

Views: 104

Answers (5)

jimhark
jimhark

Reputation: 5046

Make this change:

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.")

You want the print statements outside the loop.

I'd probably write this like:

hasUpper = any([c.isupper() for c in username])
if hasUpper:
    print ("You have at least 1 uppercase in this username.")
else:
    print ("You have no uppercase in this username.")

Upvotes: 1

poke
poke

Reputation: 388163

Your indentation is off:

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.")

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:

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.")

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, d and e, you can just keep using c. 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:

numupper, numlower, numdigit = 0, 0, 0

for c in username:
    if c.isupper():
        numupper += 1
    if c.islower():
        numlower +=1
    if e.isdigit():
        numdigit += 1

# and now check numupper, numlower and numdigit

Also, as you can see, I removed the == True from the if conditions. It is generally a good practice to leave that out, as if already 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() == username means that there are no uppercase characters in it; similarily username.upper() == username means that there are no lowercase characters.

Upvotes: 2

Blender
Blender

Reputation: 298432

You can greatly simplify your code if you remove the try...except block and replace your loops with map()s:

import string

while True:
    errors = []
    username = input("Username: ")

    if len(username) < 8:
        errors.append("The username must be at least 8 characters long.")

    if len(map(string.isupper, username)) == 0:
        errors.append("You have no uppercase in this username.")

    if len(map(string.isupper, username)) == 0:
        errors.append("You have no lowercase in this username.")

    if len(map(string.isdigit, username)) == 0:
        errors.append("You have no digits in this username.")

    if not username.isalnum():
        errors.append("Your name can only contain alpha numeric characters.")

    if not errors:
        print("Thank you for your input")
        break
    else:
        for error in errors:
            print(error)

        print("Sorry, not valid. Try again.")

Upvotes: 0

thegrinner
thegrinner

Reputation: 12241

If you don't need the counts of uppercase/lowercase/digits, you can break out of the for loop. Like this:

    for d in username:
        if d.islower():
            print ("You have at least 1 lowercase in this username.")
            break
    else:
        # This executes only if the for loop doesn't end because of a break
        print ("You have no lowercase in this username.")

Upvotes: 0

cheeken
cheeken

Reputation: 34675

The "character count checks" lines, like if numupper > 0:, are inside of the for-each-character-in-username loop. Put them after the loop rather than inside it to effect the desired result.

e.g.,

for c in username:
        if c.isupper() == True:
            numupper += 1
# Now we're outside the loop, because we've de-dented the lines below
if numupper > 0:
    print ("You have at least 1 uppercase in this username.")
else:
    print ("You have no uppercase in this username.")

Upvotes: 1

Related Questions