次のコードがあります。ログイン機能では、出力が誤っています(論理エラー)。それは "無効なユーザー名とパスワード"が正しいものになるまで基本的に印刷し、 "正しいログイン"を表示します。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のような言語でのロジックは健全です。
私は、問題が別のコード(および非常に単純な修正)なしで修正できる場合は好きですが、おそらく最も洗練されたソリューションはフラグです。つまり、問題がインデントなどではなく、私が見逃したようなものであれば。
あなたは他の 'を実行します。プリント(「無効を..」 ) '失敗したテストごとに、そうです、あなたはその出力を得ます。 –
修正を提案できますか?誰かが非常に親切に私をdownvotedているにもかかわらず、これは、この論理/コード構造がサウンドになるVB.Netの背景から来ているという点で有効な質問です – MissComputing
2つのリストを使用する代わりにユーザー名とパスワードを保存して、代わりに辞書を使用してください。 –