Hi i have been given a task to read these numbers from within a text file "1,3,5,7,9,11,13,15," and to add them together how would i do this? So far i have:
file = open("C:\\Users\\Dylan\\Python\\OddNumbersAns.txt", "r")
line = file.read()
intline = int(line)
print(intline)
But i recieve this error:
Traceback (most recent call last):
File "C:\Users\Dylan\Python\text file add.py", line 3, in <module>
intline = int(line)
ValueError: invalid literal for int() with base 10: '1, 3, 5, 7, 9, 11, 13, 15, '
Thanks
Answer:
file = open("C:\\Users\\Dylan\\Python\\OddNumbersAns.txt", "r")
line = file.read()
line.split(",")
print (line.split(","))
total = sum([int(num) for num in line.split(',')])
print(total)
splitfunction.