Final answer:
A Python program can be written to collect student names and scores, compare inputs, and determine the top two students based on their scores. The program prompts the user for the number of students, and then for each student's name and score, calculates the highest and second-highest scores, and outputs the names and scores of the top two students.
Step-by-step explanation:
To find the top two students by score, we will write a Python program that records each student's name and score and then identifies the students with the highest and second-highest scores.
The program will first ask the user to enter the number of students. Then, it will prompt for each student's name and score, keeping track of the two highest scores and corresponding student names throughout the input process.
After all data is entered, the program will display the names and scores of the top two students. The process involves comparing each entered score with the current highest and second-highest, updating these values as necessary.
Sample Python Program:
num_students = int(input("Enter the number of students: "))
highest_score = -1
second_highest_score = -1
highest_name = ""
second_highest_name = ""
for _ in range(num_students):
name = input("Enter a student name: ")
score = float(input("Enter a student score: "))
if score > highest_score:
second_highest_score = highest_score
highest_score = score
second_highest_name = highest_name
highest_name = name
elif score > second_highest_score:
second_highest_score = score
second_highest_name = name
print("Top two students:")
print(f"{highest_name}'s score is {highest_score}")
print(f"{second_highest_name}'s score is {second_highest_score}")