简体   繁体   English

当我的 if 语句为真时,为什么 else 语句会运行?

[英]Why does the else statement run when my if statement is true?

This is a snippet of my code that is in a while loop.这是我在while循环中的代码片段。 The else statement always prints when I run this part of the code:当我运行这部分代码时,总是会打印else语句:

for i in range(len(StudentRoster)):
    if StudentRoster[i][0] == delInfo[0]:
        if StudentRoster[i][1] == delInfo[1]:
            del StudentRoster[i]
            print(StudentRoster)
    else:
        print("Student is not already added to the grading manager.")

The entire code:整个代码:

def GradeManager():
    StudentRoster = []
    while True:
        command = input("$ ")
        if command[0:10] == "AddStudent":
            AddInput = command[11:]
            AddInput = AddInput.replace(" ", "")
            StudentInfo = AddInput.split(",")
            student = (StudentInfo[0], StudentInfo[1], StudentInfo[2], StudentInfo[3])
            StudentRoster.append(student)
            print (StudentRoster)
        elif command[0:13] == "DeleteStudent":
            print("yes")
            delStudent = command[14:]
            delStudent = delStudent.replace(" ", "")
            delInfo = delStudent.split(",")
            print (delInfo)
            for i in range(len(StudentRoster)):
                if StudentRoster[i][0] == delInfo[0]:
                    if StudentRoster[i][1] == delInfo[1]:
                        del StudentRoster[i]
                        print (StudentRoster)
                else:
                    print("Student is not already added to the grading manager.")
        else:
            print("Fail")

GradeManager()

Here is the input I entered:这是我输入的输入:

$ AddStudent John, Doe, 78, 94
$ AddStudent Jean, Davis, 83, 76
$ AddStudent Aaron, Johnson, 67, 72
$ DeleteStudent Aaron, Johnson

After DeleteStudent , it prints out the else statement even though Aaron Johnson was on the student list.DeleteStudent之后,即使Aaron Johnson在学生名单上,它也会打印出else语句。

for循环迭代之一中, if StudentRoster[i][0] == delInfo[0]: has failed

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

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