Home > Back-end >  Python if elif and loops
Python if elif and loops

Time:10-18

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)

CodePudding user response:

You can simply store the details in a list. And use list.sort() to sort the values according to the score.

Here is the working code:

num_students = int(input("Enter number of students: "))
scores = []

if num_students < 2:
    print('Number of students less than 2')
    exit()

for i in range(num_students):
    name = input("Enter student's name: ")
    score = int(input("Enter student's score: "))
    scores.append([name, score])

scores.sort(key=lambda details: details[1], reverse=True)

print("Top Two Students")

print()

print(f"{scores[0][0]}'s score is {scores[0][1]}")

print(f"{scores[1][0]}'s score is {scores[1][1]}")

CodePudding user response:

Here's a solution based on my previous comments to your original post.

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:  # don't need this "if" after previous "break"
    name = input("enter student name: ")
    score = int(input("enter student score: "))
    if score > second_score:
        if score > high_score:
            second_score = high_score
            second_name = high_name  # NEW
            high_score = score
            high_name = name
        else:  # simplified to just a plain "else:"
            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)

Notice that simplifying that last "elif ...:" to a simple "else:" also solves a simple bug where entering the current high score again can be ignored and not captured. If you were to run your code as is and use input values "100 80 100", you would find the 2nd high score set to 80 instead of 100.

Happy coding!

  • Related