dimanche 17 octobre 2021

Python if elif and loops

I wrote this for my beginner class and I don't know how to get the NAME of the SECOND STUDENT with the 2nd highest score. After testing it, I believe the code works for the first student.

I think what I need is to store the highest score in to variable high_score and then the next highest in to second_score and then compare the third score to both the highest and second score. But I am confused on how to get the name of the second highest scoring student.

num_students = int(input("enter number of students: "))
high_score = 0
high_name = ""
second_name = ""
second_score = 0

for i in range(1,num_students + 1):
    if num_students < 2:
        break
    if num_students >= 2:
        name = input("enter student name: ")
        score = int(input("enter student score: "))
    if score > second_score:
        if score > high_score:
            second_score = high_score
            high_score = score
            high_name = name
        elif score < high_score:
            second_score = score
            second_name = name
print()
print("Top Two Students")

print()

print(high_name,"'s score is", high_score)

print(second_name,"'s score is", second_score)

Aucun commentaire:

Enregistrer un commentaire