42.1k views
4 votes
Find the two highest scores)

Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score and the student with the second-highest score. Assume that the number of students is at least 2.

Sample Run

Enter the number of students: 5

Enter a student name: Smith

Enter a student score: 60

Enter a student name: Jones

Enter a student score: 96

Enter a student name: Peterson

Enter a student score: 85

Enter a student name: Greenlaw

Enter a student score: 98

Enter a student name: Zhang

Enter a student score: 95

Top two students:

Greenlaw's score is 98.0

Jones's score is 96.0

PYTHON CODING

User Siphalor
by
4.0k points

2 Answers

4 votes

Final answer:

To find the top two student scores in Python, prompt the user for the number of students and gather each student's name and score. Store this data, sort by scores, and output the top two scores and the associated student names.

Step-by-step explanation:

To write a program that displays the top two student scores, we start by prompting the user for the number of students. Then we collect each student's name and score, storing them in a way that we can sort and retrieve the information when needed. We can use a list to keep track of the names and scores. By iterating through this list, we can identify the two highest scores and output the students' names along with their scores. Below is a sample code that achieves this.

num_students = int(input('Enter the number of students: '))

students = []

for _ in range(num_students):
name = input('Enter a student name: ')
score = float(input('Enter a student score: '))
students.append((score, name))

students.sort(reverse=True)

print('Top two students:')
print(f"{students[0][1]}'s score is {students[0][0]}")
print(f"{students[1][1]}'s score is {students[1][0]}")

This program will output the names and scores of the two students with the highest scores in descending order.

User Pimentel
by
4.2k points
2 votes

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}")
User Pina
by
3.6k points