SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.loops
Exercise:
Programming Excercises
Question:10 | ISBN:978013274719 | Edition: 6

Question

(Find the highest score) Write a program that prompts the user to enter the number f students and each student’s score, and displays the highest score. Assume that the input is stored in a file named score.txt, and the program obtains the input from 

the file.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Find the highest Score Program Code:

# Prompt the user to enter the number of students
numOfStudents = int(input("Enter the number of students: "))

# Initialize the variable for highest score
highestScore = None

# Read input from the file
with open("score.txt", "w") as file:
    # Write the number of students to the file
    file.write(str(numOfStudents) + "\n")

    # Prompt the user to enter each student's score and write it to the file
    for i in range(numOfStudents):
        score = int(input("Enter the score for student {}: ".format(i+1)))
        file.write(str(score) + "\n")

        # Check if the current score is higher than the highest score
        if highestScore is None or score > highestScore:
            highestScore = score

# Display the highest score
print("The highest score is:", highestScore)

Executed Output:

Enter the number of students: 5
Enter the score for student 1: 500
Enter the score for student 2: 200
Enter the score for student 3: 900
Enter the score for student 4: 400
Enter the score for student 5: 100
The highest score is: 900

Final output data in "score.txt":

5
500
200
900
400
100

 

0 0

Discussions

Post the discussion to improve the above solution.