Question: Goal: Recap knowledge learned about reading files and converting values. Assignments: A file named players.txt tracks the performance of each player this season. On one
Goal: Recap knowledge learned about reading files and converting values.
Assignments: A file named players.txt tracks the performance of each player this season. On one line the name of the player is stored, and on the following, the number of goals scored.
Write a program that reads the file, and displays the name of the player that scored the most goals. During the bookkeeping, however, errors often occur, which means some of the numbers may contain typos like O where the second character is a capital O and not a zero. Accounting for this, if you encounter an error, display the name of the player for whom the score cannot be converted with an asterisk at the end.
Sample players.txt
F Reddley
L Brukworn
M M Saad
T Morey
O
A Fox
L Bromley J
r
J S Abrahms
F Inchat
Sample Run
T Morey
L Bromley J
J S Abrahms is the MVP
My python code: but it still shows it's wrong, I don't understand. It supposed to have lines as above, but it is missing T Morey so what am I missing, what did I do wrong?
def findmvpfilename:
try:
with openfilenamer as file:
lines file.readlines
playersscores
errorplayers
i
while i lenlines:
player linesistrip
i
if i lenlines:
scorestr linesistrip
try:
# Replace O with for valid conversion
score intscorestrreplaceO
playersscoresplayer score
except ValueError:
errorplayers.appendfplayer
i
# Ensure all error players are printed in one line
if errorplayers:
printjoinerrorplayers
if playersscores:
mvp maxplayersscores, keyplayersscores.get
printfmvp is the MVP
except FileNotFoundError:
printCould not find the file."
except Exception as e:
printfAn error occurred: e
# Call the function with the name of your file
findmvpplayerstxt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
