I'm working on a code to input and output files on Python, but I don't know where I got it wrong crying

Asked 2 years ago, Updated 2 years ago, 118 views

`python

>

inputfile=open("ALE.txt","r")

outputfile=open("ALE2.txt","w")

list1=[]

for line in inputfile:

Team,Won,Lost=line.split(',')
Won=int(Won)
Lost=int(Lost)
Percentage=round((Won)/(Won+Lost),3)
list1.append([Percentage,Team,Won,Lost])

list1.sort(reverse=True)

print("Team\tWon\tLost\tPercentage")

for i in range(len(list1)):

outputfile.write("{0:<10},{1:<10},{2:<10},{3:<10}".format(list1[i][2],list[i][3],list[i][4],list[i][0]))

inputfile.close() outputfile.close()


The data in ALE is as follows. In the problem, we added 'list.sort(reverse=True)' because we had to sort Percentage in descending order.

``` The purpose of the problem is to transfer the data in ALE to ALE2 like the picture above. The contents of ALE are the same as below, but there is no space between the lines.

Baltimore,93,69

Boston,69,93

New York,95,67

Tampa Bay,90,72

Toronto,73,89

I would really appreciate it if you could tell me which part was wrong and how to correct it. ㅠ<

file-io pointer list python

2022-09-22 18:39

1 Answers

When you ask a question, it is easy to answer if you write down the method of attempt, the results, the expected results, and the criteria you thought were wrong. Unfortunately, there is no information about the criteria that I thought had a problem.

First of all, I think you're asking ALE2.txt how to solve the percentage, Team Won Lost Percentage that is not printed in this file, and how to solve the problem of not dropping the line.

Before you start, the percentage is in the [0,100] range and not in the [0,1] range. There's something wrong with this part. I'll move on to this part and explain.

    Won=int(Won)
    Lost=int(Lost)
    Percentage=round((Won)/(Won+Lost),3)

If you look at the code above, Won and Lost are integers (int). In Python, the result of an integer division integer becomes an integer. Therefore, the result of (Won)/(Won+Lost) will be 0 or 1.

Won and Lost need to be real numbers to calculate the desired value. Therefore, you can modify it as below.

    Won=float(Won)
    Lost=float(Lost)
    Percentage=round((Won)/(Won+Lost),3)

The Team Won Lost Percentage header should be output to ALE2.txt, but if you look at the code, you can see that it is output to stdout as shown below.

print("Team\tWon\tLost\tPercentage")

Therefore, you can fill out the following so that it is output to outputfile which is the file object of ALE2.txt.

outputfile.write("{:<10}{:<10}{:<10}{:<10}\n".format("Team", "Won", "Lost", "Percentage"))

write() does not automatically drop one line. Therefore, you must enter \n for a single line down. Also, there is no and in the result picture, so I'll erase it.

for i in range(len(list1)):

    outputfile.write("{0:<10}{1:<10}{2:<10}{3:<10}\n".format(list1[i][1],list1[i][2],list1[i][3],list1[i][0]))

Unlike the first question, format(list1[i][2], list[i][3], list[i][4], list[i][0]) was written incorrectly, so I arbitrarily corrected it.

Applying the above-mentioned modification method is as follows.

inputfile=open("ALE.txt","r")

outputfile=open("ALE2.txt","w")

list1=[]

for line in inputfile:

    Team,Won,Lost=line.split(',')
    Won=float(Won)
    Lost=float(Lost)
    Percentage=round((Won)/(Won+Lost),3)
    list1.append([Percentage,Team,Won,Lost])

list1.sort(reverse=True)

outputfile.write("{:<10}{:<10}{:<10}{:<10}\n".format("Team", "Won", "Lost", "Percentage"))

for i in range(len(list1)):

    outputfile.write("{0:<10}{1:<10}{2:<10}{3:<10}\n".format(list1[i][1],list1[i][2],list1[i][3],list1[i][0]))
inputfile.close()
outputfile.close()


2022-09-22 18:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.