2017-06-30 20 views
1

次のコードがあります。ログイン機能では、出力が誤っています(論理エラー)。それは "無効なユーザー名とパスワード"が正しいものになるまで基本的に印刷し、 "正しいログイン"を表示します。forループ内のネストされたif文のインデントエラー

誤った出力

例えば、テストデータで:USER3とPASS3、出力がある:ここ

*****LOGIN SCREEN****** 
Username: user3 
Password: pass3 
invalid username or password 
invalid username or password 
correct login 
>>> 

は、問題のコードログインを参照して、あります関数:

usernames=["user1","user2","user3"] 
passwords=["pass1","pass2","pass3"] 

def main(): 
    mainmenu() 


def mainmenu(): 
    print("****MAIN MENU****") 
    print("=======Press L to login :") 
    print("=======Press R to register :") 
    choice1=input() 
    if choice1=="L" or choice1=="l": 
     login() 
    elif choice1=="R" or choice1=="r": 
     register() 
    else: 
     print("please make a valid selection") 

usernames=["user1","user2","user3"] 
passwords=["pass1","pass2","pass3"] 

def login(): 
    print("*****LOGIN SCREEN******") 
    username=input("Username: ") 
    password=input("Password: ") 
    for index_of_current_user, current_user in enumerate(usernames): #enumerate allows to you to go throw the list and gives to you the current element, and the index of the current element 
    if username == current_user and passwords[index_of_current_user] == password: #since the two list are linked, you can use the index of the user to get the password in the passwords list 
     print("correct login") 
    else: 
     print("invalid username or password") 

def register(): 
    print("*****REGISTRATION****") 
    username=input("Enter a username:") 
    password=input("Enter a password:") 
    users_pass[username] = password 
    answer=input("Do you want to make another registration?") 
    if answer=="y": 
    register() 
    else: 
    registration_details() 

def registration_details(): 
    print(usernames) 
    print(passwords) 

main() 

私が探しているのは です)ソリューションを、それが唯一の「正しいログイン」一度ユーザ名とパスワードの正しいペアを見つけるの代わりをループして理由を各1つの

B)説明を印刷するに印刷するような問題にを修正字下げやそれが間違っているものはここでは機能しません - VB.Netのような言語でのロジックは健全です。

私は、問題が別のコード(および非常に単純な修正)なしで修正できる場合は好きですが、おそらく最も洗練されたソリューションはフラグです。つまり、問題がインデントなどではなく、私が見逃したようなものであれば。

+1

あなたは他の 'を実行します。プリント(「無効を..」 ) '失敗したテストごとに、そうです、あなたはその出力を得ます。 –

+0

修正を提案できますか?誰かが非常に親切に私をdownvotedているにもかかわらず、これは、この論理/コード構造がサウンドになるVB.Netの背景から来ているという点で有効な質問です – MissComputing

+0

2つのリストを使用する代わりにユーザー名とパスワードを保存して、代わりに辞書を使用してください。 –

答えて

1

あなたはループを実行した後に印刷できるよう、あなたは、リストのすべての要素のための印刷:

def login(): 
    print("*****LOGIN SCREEN******") 
    username=input("Username: ") 
    password=input("Password: ") 
    correct_login = False 
    for index_of_current_user, current_user in enumerate(usernames): #enumerate allows to you to go throw the list and gives to you the current element, and the index of the current element 
    if username == current_user and passwords[index_of_current_user] == password: #since the two list are linked, you can use the index of the user to get the password in the passwords list 
     correct_login = True 
     break 
    if(correct_login): 
    print("correct login") 
    else: 
    print("invalid user name or password") 
2

これは、ログイン機能のelseステートメントが原因で発生しています。基本的にあなたのコードが現在その機能で行っていることはループスルーし、ユーザー名とパスワードが現在の値と等しいかどうか(つまり、user1 == user2を比較する)をチェックして、

無効なユーザー名またはパスワードメッセージを印刷するために、すべての値を比較するまで待つ必要があります。また、値をループし続ける代わりに、有効な値が見つかるとforループを停止させることができます。あなたは正しいログインまたは無効なユーザー名またはパスワードのいずれかの1つだけのインスタンスを与える

def login(): 
    print("*****LOGIN SCREEN******") 
    username=input("Username: ") 
    password=input("Password: ") 
    found = False 
    for index_of_current_user, current_user in enumerate(usernames): #enumerate allows to you to go throw the list and gives to you the current element, and the index of the current element 
    if username == current_user and passwords[index_of_current_user] == password: #since the two list are linked, you can use the index of the user to get the password in the passwords list 
     print("correct login") 
     found = True 
     break 
    if found == False: 
    print("invalid username or password") 

:あなたのログイン機能は、次のようになります。

+0

それは問題を解決する、私はupvote、しかし私は見つける必要はありません== Falseの場合は、Falseは既にブール値であり、真実から、もしあなたが欲しいのであれば、上記の私の答えを見てください。あなたは –

3

このように例外を利用してログインを書き込むことができます。ログイン()関数で

def login(): 
    print("*****LOGIN SCREEN******") 
    username=input("Username: ") 
    password=input("Password: ") 
    try: 
    index = usernames.index(username) 
    if password == passwords[index]: 
     print("correct login") 
    else: 
     print("invalid username or password") 
    except: 
    print("invalid username or password") 
+0

という興味深い選択肢があります。私はそれについて考えたことはありません –

関連する問題