2016-05-18 225 views
0

我应该编写一个验证用户名的程序。用户名必须包含至少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: ") 
+0

究竟什么是你的问题反驳他们? –

+0

这个早期的帖子应该会帮助你: http://stackoverflow.com/questions/6602111/how-to-search-for-a-capital-letter-within-a-string-and-return-the-list-字词 – nbryans

+1

你为什么使用键码?你知道,显然有更好的方法来做到这一点。 – Laurel

回答

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

如果它没有大写字母,小写字母或至少一个数字,则程序需要重新提示用户。 –

+0

@PranayPatel完成,检查我的编辑 – Keatinge

+0

有没有其他方式哈哈?就像你可以把输入的用户名,通过一个循环,并检查是否有ATLEAST一个大写字母? –

相关问题