2016-05-18 65 views
0

ユーザー名を確認するプログラムを作成することになっていました。ユーザー名は8〜15文字以上で、英数字の値は使用できません。だから、あなたは数字と文字だけを持つことができます。最初または最後に数字を付けることはできません。そして、少なくとも1つの大文字と小文字と少なくとも1つの数字を持っていなければなりません。私はすべてのことをする方法を持っていますが、プログラムに大文字が含まれているかどうかを確認する方法を知っています。私は "ではない"しかし運がないことを試みた。これは私がこれまで持っていたものです。文字列に大文字が含まれているかどうかを確認してください。

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

大文字、小文字、または少なくとも1つの数字がない場合は、プログラムを再呼び出しする必要があります。 –

+0

@PranayPatel done、私の編集を確認してください – Keatinge

+0

他の方法がありますか?あなたは入力ユーザー名を取ることができますか?それをループに入れて、ATLEASTの大文字が1つあるかどうかを確認しますか? –

関連する問題