Sum numbers


#1



I need to sum the objects in a text file. It's almost working, but I'm receiving an error that says 'invalid literal for int() wih base 10: 4.2' and can't figure out what I need to do?


def addNumbersAndSave(textfile,newFile):
    list1 = []
    sum1 = 0
    myfile = open(textfile)
    for i in myfile:
        list1.append(i.strip())
    for i in list1:
        i = int(i)
        sum1 += i
    f = open(newFile,"w")
        
    f.write(str(sum1))       
    f.close()
    myfile.close()


#2

Can you post a screenshot of the error? I have run your code in the codecademy environment as well as another environment (https://goo.gl/Gkm9ZE) and it seems to run fine, so I can't reproduce the problem.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.