2017-11-16 8 views
-1

私は学校のプロジェクト(変数名について説明しています)にこのコードを持っていますが、forループの最下位に行くとリストのインデックスが範囲外です。パスワードを尋ねる前に、ユーザー名を入力してチェックすることになっています。私は何が間違っているのか理解しようとしましたが、何もできません。forループからのIndexErrorを理解しない

def passwordSystem(): 
    #TASK ONE 
    username_array = [] 
    password_array = [] 
    for i in range (1, 30): 
     username_to_be_entered = str(input("Enter a username")) 
     acceptable_password = False 
     while not acceptable_password: 
      password_to_be_entered = str(input("Enter a password between 6 and 12 characters")) 
      if len(password_to_be_entered) > 12 or len(password_to_be_entered) < 6: 
       print ("Your password was not between 6 and 12 characters") 
      else: 
       acceptable_password = True 
       print ("Password accepted") 
     print ("Your username is:" ,username_to_be_entered) 
     print ("Your password is:" ,password_to_be_entered) 
    #TASK TWO 
    username_accepted = False 
    while not username_accepted: 
     username_to_be_checked = str(input("Please enter your username")) 
     for i in range (1, 30): 
      if username_array[i] == username_to_be_checked: 
       print ("Username accepted") 
       username_accepted = True 
+1

完全なエラーメッセージを投稿できますか? –

+0

自分で配列全体を反復処理する必要はありません。 'in'を使うだけです。 – wpercy

+1

usernameの配列は永遠に空のままなので、' username_array [i] 'をチェックしようとすると、配列の終わりを常に過ぎます。 – turbulencetoo

答えて

1

あなたのリストは永遠に残ります。このコードブロックでは、ユーザー名とパスワードをそれぞれのリストに追加する必要があります。

while not acceptable_password: 
    password_to_be_entered = str(input("Enter a password between 6 and 12 characters")) 
    if len(password_to_be_entered) > 12 or len(password_to_be_entered) < 6: 
     print ("Your password was not between 6 and 12 characters") 
    else: 
     acceptable_password = True 
     username_array.append(username_to_be_entered) 
     password_array.append(password_to_be_entered) 
     print ("Password accepted") 

はその後、ユーザー名を確認するために、あなただけ手動でリスト全体を反復処理するのではなく、 inキーワードを使用する必要があります。

#TASK TWO 
username_accepted = False 
while not username_accepted: 
    username_to_be_checked = str(input("Please enter your username")) 
    if username_to_be_checked in username_array: 
     print ("Username accepted") 
     username_accepted = True 
関連する問題