I am building a function that opens a file, calculates the sum of integers on different lines, and appends the file with a new string 'Total=sum' on a new line. I am getting error: can't assign to operator on my final value. This is not a duplicate being that I edited the way the duplicate suggestion did and it is still throwing an error. Also, I need to KEEP THE TOTAL, after every iteration.
Here is my function:
def append_total(filename):
total=0
with open(filename) as contents:
for line in contents:
if line.isdigit():
total+=int(line)
else:
total=0
final='Total:'+total+end='\n'
contents.write(final)
return