Python 检查字符串中是否存在大写字母
我应该写一个程序来验证用户名。用户名必须至少包含8-15个字母,不包含字母数字值。所以,你只能有数字和字母。开头或结尾不能有数字。而且,您必须至少有一个大写字母和小写字母以及至少一个数字。我知道如何做所有事情,但如何让程序检查它是否包含任何大写字母。我试着做“如果不在”,但没有运气。这就是我目前所拥有的Python 检查字符串中是否存在大写字母,python,Python,我应该写一个程序来验证用户名。用户名必须至少包含8-15个字母,不包含字母数字值。所以,你只能有数字和字母。开头或结尾不能有数字。而且,您必须至少有一个大写字母和小写字母以及至少一个数字。我知道如何做所有事情,但如何让程序检查它是否包含任何大写字母。我试着做“如果不在”,但没有运气。这就是我目前所拥有的 username = input("please enter a name: ") for i in username: while len(username) < 8 or le
username = input("please enter a name: ")
for i in username:
while len(username) < 8 or len(username) > 15:
print("Password is too long or too short")
username = input("please enter a name: ")
j = 31
while j < 47:
j += 1
while chr(j) in i:
print("No alphanumeric values allowed.")
username = input("please enter a name: ")
k = 57
while k < 64:
k += 1
while chr(k) in i:
print("No alphanumeric values allowed.")
username = input("please enter a name: ")
l = 47
while l < 57:
l += 1
while chr(l) in username[0]:
print("you cannot have a number in the beggining")
username = input("please enter a name: ")
while chr(l) in username[-1]:
print("you cannot have a number in the end")
username = input("please enter a name: ")
username=input(“请输入名称:”)
对于用户名中的i:
而len(用户名)<8或len(用户名)>15:
打印(“密码太长或太短”)
用户名=输入(“请输入名称:”)
j=31
而j<47:
j+=1
而chr(j)在i中:
打印(“不允许使用字母数字值”)
用户名=输入(“请输入名称:”)
k=57
当k<64时:
k+=1
而chr(k)在i中:
打印(“不允许使用字母数字值”)
用户名=输入(“请输入名称:”)
l=47
当l<57时:
l+=1
而用户名[0]中的chr(l):
打印(“列表中不能有数字”)
用户名=输入(“请输入名称:”)
而用户名[-1]中的chr(l):
打印(“结尾不能有数字”)
用户名=输入(“请输入名称:”)
您可以在生成器中使用any来测试字符串是否有大写字母
testString = "abjKcf"
print(any(x.isupper() for x in testString)) #true
好办法 至于解决问题的方法,欢迎来到生成器表达式和断言的世界
while True:
testString = input()
try:
assert 8 <= len(testString) <= 15, "String must be between 8 and 15 characters"
assert all(x.isalnum() for x in testString), "String must be alphanumeric"
assert any(x.isupper() for x in testString), "String must contain one capital"
assert any(x.islower() for x in testString), "String must contain one lowercase"
assert any(x.isdigit() for x in testString), "String must contain one digit"
assert testString[0].isdigit() == False, "No numbers at start"
assert testString[-1].isdigit() == False, "No numbers at end"
break #if we haven't hit any errors then the username fits the criterion
except Exception as e:
print(e)
为True时:
testString=input()
尝试:
断言8你的问题到底是什么?这篇早前的文章应该会帮助你:你为什么要使用关键代码?显然有更好的方法可以做到这一点,你知道。你说它必须没有字母数字值,只有字母和数字?这些陈述是矛盾的,我想你的意思是它必须只有字母数字值?是我的错。我的意思是它只能有字母数字值。如果它没有大写字母、小写字母或至少一个数字,那么程序需要重新编译用户。还有其他方法吗?比如你能把输入的用户名,通过一个循环,检查是否至少有一个大写字母吗?是的,这就是代码的作用。你不喜欢它因为它太简单了吗?你想要老式的丑陋的环?这基本上是检查每一个字母,确保其中一个字母是大写的,它就在场景后面哈哈哈,我还没有学到那么先进的东西,所以我仍然不知道它到底是什么。我如何使用丑陋的for循环来达到同样的结果哈哈哈
while True:
testString = input()
allAlphaNumeric = True
oneCapital = False
oneLowerCase = False
oneDigit = False
for letter in testString:
if not letter.isalnum():
oneAlphaNumeric = False
if letter.isupper():
oneCapital = True
if letter.islower():
oneLowerCase = True
if letter.isdigit():
oneDigit = True
numberAtStart = testString[0].isdigit()
numberAtEnd = testString[-1].isdigit()
if allAlphaNumeric and oneCapital and oneLowerCase and oneDigit and not numberAtEnd and not numberAtStart:
break
if not 8 <= len(testString) <= 15:
print("String must be between 8 and 15 characters")
if not allAlphaNumeric:
print("Your string must be alphanumeric!")
if not oneCapital:
print("Your string must contain at least one capital letter")
if not oneLowerCase:
print("Your string must contain atleast one lowercase letter")
if not oneDigit:
print("Your string must contain atleast one digit")
if numberAtStart:
print("You cannot have a number at the start")
if numberAtEnd:
print("You cannot have a number at the end")