for循环中的嵌套if语句中的特定缩进错误 [英] Specific Indentation error in nested if statement in for loop

查看:458
本文介绍了for循环中的嵌套if语句中的特定缩进错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,并且在登录功能中,输出是错误的(一个逻辑错误)。它基本上打印无效的用户名和密码,直到它到达正确的,然后打印正确的登录。


$ b

错误输出

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

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $用户名:user3
密码:pass3
无效的用户名或密码
无效的用户名或密码
正确的登录
>>>

以下是参考LOGIN函数的代码

  usernames = [user1,user2,user3] 
passwords = [pass1, ():pass2,pass3]

def main():
mainmenu()

$ b def mainmenu():
print (**** MAIN MENU ****)
print(=======按L登录:)
print(=======按R来注册:)
choice1 = input()
if choice1 ==Lor choice1 ==l:
login()
elif choice1 == R或者choice1 ==r:
register()
else:
print(请做一个有效的选择)

usernames = [user1 ()*,user2,user3]
passwords = [pass1,pass2,pass3]

def login **** LOGIN SCREEN ******)
username = input(Username:)
password = input(Password:)
for index_of_current_user,current_user in enumerate (用户名):#enumerate允许你去扔列表,并给你的立方米rrent元素和当前元素的索引
if username == current_user和passwords [index_of_current_user] == password:#因为这两个链表是链接的,所以可以使用用户索引获取密码密码列表
print(正确登录)
else:
print(invalid username or password)

def register():
print (***** REGISTRATION ****)
username = input(输入用户名:)
password = input(输入密码:)
users_pass [如果答案==y:
register()
else:
$ b答案=输入(你想再次注册吗? $ b registration_details()
$ b $ def registration_details():
print(username)
print(passwords)
$ b $ main()

我正在寻找
a)解决方案并修复问题,只有在找到正确的用户名和密码对之后才打印正确的登录名 rds,而不是通过循环和打印每一个

b)一个解释为什么缩进或者这是什么错误在这里doesn'工作 - 因为逻辑(像VB.Net这样的语言)是健全的。



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

解决方案

在您的登录)函数,你打印列表中的每一个元素,所以你可以在循环之后打印:

  def login(): 
print(***** LOGIN SCREEN ******)
username = input(Username:)
password = input(Password:)
correct_login = False
为index_of_current_user,current_user为enumerate(用户名):#enumerate允许你抛出列表并给你当前元素,以及当前元素的索引
如果用户名== current_user和passwords [index_of_current_user] == password:#since这两个列表是链接的,你可以使用用户的索引来获取密码列表中的密码
correct_login = True
break
if(correct_login):
print(correct login)
else:
print(invalid user name or password)


I have the following code and in the login feature, the output is erroneous (a logic error). It basically prints "invalid username and password" until it gets to the right one, and then prints "correct login".

ERRONEOUS OUTPUT:

For example, with the test data: user3 and pass3, the output is:

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

Here is the code in question, with reference to the LOGIN function:

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

I am looking for a) a solution and fix to the problem so that it only prints "correct login" once on finding the correct pair of usernames and passwords instead of looping through and printing each one

b) an explanation as to why the indentation or whatever it is that is wrong here doesn't work - as the logic (in a language like VB.Net) is sound.

I'd prefer it if the problem could be fixed without additional code (and a very simple fix) but perhaps the most elegant solution is a flag. That is, if the problem isn't just with indentation or something like that I've missed.

解决方案

In your login() function, you print for every element of the list, so you could print after the loop doing:

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

这篇关于for循环中的嵌套if语句中的特定缩进错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆