这是我目前的代码:
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
如何更改我的代码,以便它只显示诸如“此用户名中至少包含小写”的语句一次。非常感谢你
答案 0 :(得分: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循环使用相同的变量。因此,您无需使用c
,d
和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
另外,正如您所看到的,我从if条件中删除了== True
。一般情况下保留它是一种好习惯,因为if
已经检查表达式是否等于true。所以它基本上是多余的。
最后,你可以稍微改变一下这些检查。例如,您可以通过将字符串转换为相同的字符串来检查小写和大写字符,并将其与原始文本进行比较。所以username.lower() == username
表示其中没有大写字符;类似username.upper() == username
意味着没有小写字符。
答案 1 :(得分: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.")
答案 2 :(得分: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.")
答案 3 :(得分: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.")
答案 4 :(得分:0)
如果您移除try...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.")