jeudi 22 juillet 2021

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. The else statement always prints when I run this part of the code:

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.

Aucun commentaire:

Enregistrer un commentaire