繁体   English   中英

为什么我的子例程在调用另一个子例程后仍继续执行我的所有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部分。 我似乎无法理解为什么这样做,但实际上却停止了我的进度。 在此处输入图片说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM