Design a solution that requests and receives student names and an exam score for each.
The program should continue to accept names and scores until the user inputs a student whose name is “alldone”.
***After the inputs are complete determine which student has the highest score and display that student’s name and score.
8 years ago
1 year ago
To design a solution for this task, you can follow these steps:
1. Initialize an empty dictionary to store the student names and their exam scores.
2. Use a loop that repeats until the user inputs a student with the name "alldone."
3. Inside the loop, prompt the user to enter a student name and their exam score.
4. Store the name as the key in the dictionary and the score as the corresponding value.
5. If the user enters "alldone" as the student name, break out of the loop to stop taking inputs.
6. After the inputs are complete, determine the student with the highest score by using the `max()` function on the dictionary values.
7. Retrieve the name of the student with the highest score using the `index()` method of the dictionary values.
8. Print the name and score of the student with the highest score.
Here's an example implementation in Python:
```python
student_scores = {}
while True:
student_name = input("Enter student name (or 'alldone' to exit): ")
if student_name == "alldone":
break
exam_score = int(input("Enter exam score: "))
student_scores[student_name] = exam_score
highest_score = max(student_scores.values())
best_student_name = list(student_scores.keys())[list(student_scores.values()).index(highest_score)]
print("The student with the highest score is", best_student_name, "with a score of", highest_score)
```
Now, the program will repeatedly prompt the user for student names and scores until they enter "alldone." It will then determine and display the student with the highest score.