for循环中嵌套if语句中的特定缩进错误

Specific Indentation error in nested if statement in for loop

我有以下代码,在登录功能中,输出错误(逻辑错误)。它基本上打印 "invalid username and password" 直到它到达正确的那个,然后打印 "correct login"。

错误输出

例如,测试数据:user3和pass3,输出为:

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

这里是有问题的代码,参考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()

我正在寻找 a) 解决方案并修复 问题,这样它只在找到正确的用户名和密码对时打印 "correct login" 一次,而不是循环打印每个 [=12] =]

b) 解释 为什么这里的缩进或任何错误在这里不起作用 - 作为逻辑(在像 VB.Net 这样的语言中) 是健全的。

如果问题可以在没有额外代码的情况下解决(并且非常简单的修复),我会更喜欢它,但也许最优雅的解决方案是一个标志。也就是说,如果问题不仅仅是缩进或我错过的类似问题。

您之所以看到这种情况,是因为登录函数中的 else 语句。基本上你的代码当前在该函数中所做的是循环,检查用户名和密码是否等于当前值(即比较 user1 == user2)如果它们不相等,那么你会自动打印无效的用户名。

相反,您应该等到比较所有值后才打印无效的用户名或密码消息。此外 - 您可以添加一个中断以在找到有效值后停止 for 循环,而不是继续循环遍历您的值。您的登录功能将如下所示:

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

这只会给您 1 个正确登录或无效用户名或密码的实例。

您可以利用异常以这种方式编写登录。

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

在您的 login() 函数中,您打印列表中的每个元素,因此您可以在循环后打印:

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