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:
.functions
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 of 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:

# Open the file in read mode
with open("score.txt", "r") as file:
    # Read the number of students from the first line
    numOfStudents = int(file.readline().strip())

    # Initialize variable for the highest score
    highestScore = -1  # Initialize to a low value

    # Iterate over the scores in the file
    for line in file:
        # Strip leading/trailing whitespace and convert to float
        score = float(line.strip())

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

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

Data in input file "score.txt": This file have the number of students and each student’s score.

5 
100
88
66
95
75

Executed Output:

The highest score is: 100.0

 

0 0

Discussions

Post the discussion to improve the above solution.