为什么我的子例程在调用另一个子例程后继续执行我所有的 if 语句

Why does my sub-routine continue to carry out all of my if statements after I've called another sub-routine

我的 logins() 子例程在找到登录名并验证密码后将继续执行 else 和 elif 部分。我似乎无法理解为什么要这样做,但确实阻碍了我的进步。 enter image description here

def login ():
Username_Input = input("Please enter your username : ")
logins = open("logins.csv","r")
List_Information = list(csv.reader(logins))
for x in List_Information:# loops through all lists
    if x[0] != Username_Input :
        print("Username not found please register ")
        register () 
    else:
        Password_Input = input("Username found please enter your password : ")
        for x in List_Information:
            if x[1] == Password_Input :
                print("Loged in lets get this game going. ")
                game()
            else :
                print("nope sorry password not found lets go back to the menu : ")
                Menu()

找到正确的密码后,它将在调用 game() 后继续通过 List_information。对 game() 的调用不会停止循环,因此它从 List_information 中找到下一个用户并说密码错误。

您应该从 List_information 中找到正确的条目(基于用户名)并根据该条目检查密码。现在你基本上只比较 List_information.

中的第一个元素

像这样:

user = None
for x in List_information:
  if x[0] == Username_input:
    user = x
if user == None:
  print("Username not found please register ")
  register ()
else:
  Password_Input = input("Username found please enter your password : ")
  if user[1] == Password_input:
    game()
  else:
    Menu()