(Find the two highest scores) Write a program that prompts the user to enter the number of students and each student’s score, and displays the highest and secondhighest scores.
Find the two highest scores Program Code:
# Prompt the user to enter the number of students
numOfStudents = int(input("Enter the number of students: "))
# Initialize variables for highest and second highest scores
highestScore = -1 # Initialize to a low value
secondHighScore = -1 # Initialize to a low value
# Iterate over the range of students
for i in range(numOfStudents):
# Prompt the user to enter the score for each student
score = float(input("Enter the score for student {}: ".format(i+1)))
# Check if the current score is higher than the highest score
if score > highestScore:
secondHighScore = highestScore # Update the second highest score
highestScore = score # Update the highest score
# Check if the current score is higher than the second highest score
elif score > secondHighScore and score != highestScore:
secondHighScore = score # Update the second highest score
# Display the highest and second highest scores
print("The highest score of the student is:", highestScore)
print("The second highest score of the student is:", secondHighScore)
Executed Output:
Enter the number of students: 5
Enter the score for student 1:
100
Enter the score for student 2:
50
Enter the score for student 3:
99
Enter the score for student 4:
45
Enter the score for student 5:
88
The highest score of the student is: 100.0
The second highest score of the student is: 99.0