when executing on the command line it is supposed to have python3 a4.py apple
the file whose numbers i am trying to sum looks exactly like this
4 14
5
this is what i have gathered so far
import sys
print(sys.argv[1])
fp = open(sys.argv[1])
fileContents = fp.read()
fp.close()
numbers = fileContents
print(numbers)
def map(f,items):
result = []
for i in range(0,len(items),1):
result = result + [f(items[i])]
return result
a=map(eval,numbers)
def sum(num):
total = 0
for i in range(0,len(num),1):
total = total + i
return total
print(sum(a))
(I am a bit confused by the “recursion” tag)
How about this?
Notes:
float()rather thanint()withautomatically closes the file for you when you are done or you encounter an execption.Update:
You can shorten this even more with this provided by a helpful comment by J.F.Sebastian below: