检查字符串中是否存在大写字母

时间:2016-05-18 00:20:39

标签: python

我应该编写一个验证用户名的程序。用户名必须至少包含8-15个字母,不包含字母数字值。所以,你只能有数字和字母。你不能在开头或结尾有数字。并且,您必须至少有一个大写和小写字母以及至少一个数字。我得到了如何做的一切,但如何让程序检查,看它是否包含任何大写字母。我试图做“如果没有”,但没有运气。这是我到目前为止所做的。

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

1 个答案:

答案 0 :(得分:1)

你可以使用任何一个生成器来测试字符串是否有大写字母

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)

非常难看的解决方案(根据要求)

基本上你设置了一些布尔值并尝试通过循环遍历每个字符并检查它是否满足某些条件来反驳它们

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