Question

The file scores.txt (attached above) contains a list of names and test scores. Create a program...

The file scores.txt (attached above) contains a list of names and test scores. Create a program that reads each line of the file and determines the highest score and who obtained it.

This is an exercise in strings. You are required to use at least one string method to determine the names and the scores.

Your output should resemble the following:

Highest Score: 100
Achieved by: Jay

The text file looks like this:

Jan  86 
Drew  92 
Blake  85 
Alex  81 
Taylor  88 
Jordan  72
Cam  89

I know how to open the file but I am having a really hard time getting it to return what it needs too

Homework Answers

Answer #1

The code below is well commented to understand.

# variable to store highest score and the respective name
highest_score, name = 0, None
file_path = 'scores.txt'
# opening file using with open, change the path accordingly
with open(file_path) as f:
    # reading the content of file line by line
    for line in f:
        # removing the new line character by using rstrip method
        line = line.rstrip()
        # splitting the line using split method on " " as a delimeter
        line = line.split(" ")
        # checking highest score and assigning name & highest_score accordingly
        if int(line[1]) > highest_score:
          name = line[0]
          highest_score = int(line[1])

print("Highest Score: ", highest_score)
print("Achieved by: ", name)


Output:


scores.txt:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT