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

2024-10-04 01:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在while循环中的代码片段。当我运行这部分代码时,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.")

整个代码:

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

以下是我输入的输入:

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

DeleteStudent之后,它打印出else语句,即使Aaron Johnson在学生列表上


Tags: 代码ifelsecommandprintaaronwhilejohnson