0

这是我当前的代码:

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

当我使用我的主程序运行这个定义时:

import uservalidation


username = input("Username: ")


result, reason = uservalidation.valid_username(username)


if not(result):
    print (reason)

我明白了(取决于我输入了多少个字母):

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

如何更改我的代码,使其仅显示诸如“您在此用户名中至少有小写字母”之类的语句一次。非常感谢

4

5 回答 5

2

您的缩进已关闭:

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

因此,对于每个字符,您都在打印错误文本。您想要做的是将检查移动到循环之外的数字:

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

因此,在计算完大写字符后(在循环中),您评估结果并打印一次错误文本。

这同样适用于小写和数字检查。

顺便提一句。您可以对不同的 for 循环使用相同的变量。所以你不需要使用cd并且e,你可以继续使用c。此外,您应该考虑合并循环。在这三个中,您循环遍历用户名的字符,因此您可以同时执行所有操作:

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

此外,如您所见,我== True从 if 条件中删除了。通常将其排​​除在外是一个好习惯,因为if已经检查了表达式是否等于 true。所以基本上是多余的。

最后,您可以稍微不同地进行这些检查。例如,您可以通过将字符串转换为相同的字符串并将其与原始文本进行比较来检查大小写字符。Sousername.lower() == username表示其中没有大写字符;同样username.upper() == username意味着没有小写字符。

于 2012-12-05T19:49:03.830 回答
1

“字符计数检查”行,如if numupper > 0:,位于 for-each-character-in-username 循环内。将它们放在循环之后而不是内部以产生所需的结果。

例如,

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.")
于 2012-12-05T19:46:20.740 回答
1

进行此更改:

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

您想要print循环之外的语句。

我可能会这样写:

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.")
于 2012-12-05T19:50:28.147 回答
0

如果您不需要大写/小写/数字的计数,则可以跳出 for 循环。像这样:

    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.")
于 2012-12-05T19:50:04.767 回答
0

如果删除块并用stry...except替换循环,则可以大大简化代码:map()

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.")
于 2012-12-05T19:55:42.830 回答