2021年5月1日星期六

python file output bugs

So I have to take the numbers from a certain file containing:

1 5  2 300  3 3  9 155  7 73  7 0  

Multiply them and add them to a new file I used the script under here but for some reason, it now gives a syntax error.

f=open('multiply.txt')  f2=open('resulted.txt','w')    while True:      line=f.readline()      if len(line)==0:          break      line=line.strip()      result=line.split(" ")      multiply=int(result[0])*int(result[1])      multiply=str(multiply)      answer=print(result[0],"*",result[1],"=",multiply)  f2.write(str(multiply))             f.close()  f2.close()  

i found out that f2.write(multiply) works but i get all the answers as 1 string (5600913955110) how do i get it to be 1 good text file and give the right calculation

Update:

f=open('multiply.txt')  f2=open('result.txt','w')  while True:      line=f.readline()      if len(line)==0:          break      line=line.strip()      result=line.split(" ")      multiply=int(result[0])*int(result[1])      multiply=str(multiply)      answer=print(result[0],"*",result[1],"=",multiply)      answer=str(answer)      f2.write(str(answer))      f2.write(str(multiply))          f.close()  f2.close()  

output:

None5None600None9None1395None511None0  
https://stackoverflow.com/questions/67349657/python-file-output-bugs May 02, 2021 at 02:30AM

没有评论:

发表评论