Please help me understand what’s happening here. My aim is to create a function that will read “input.txt” and return the min, max, and average for each line within the text document. The text within the document is as follows:
min:1,2,3,4,5,6 max:1,2,3,4,5,6 avg:1,2,3,4,5,6
My code looks like this:
import re def process(): file = open("input.txt", "r") for line in file: newL = re.findall("d+", line) minimum = min(newL) maximum = max(newL) length = len(newL) numSum = sum(newL) print newL print minimum print maximum print length print numSum file.close() process()
Everything prints out fine except for numSum, which gives the error mentioned in the heading.
Advertisement
Answer
re.findall
gives you back a list of strings, something like ["foo", "bar", "baz"]
. The implementation of sum
is something like this:
def sum(xs): total = 0 for x in xs: total += x return total
So at some point, it’ll attempt to execute a line like total = 0 + "foo"
and fall over, since Python doesn’t know how to add an int
to a string
. The fact that you know all your strings actually contain int
s is immaterial, since the expression 1+'1'
might sensibly evaluate to either '11'
or 2
, and Python won’t guess which.
As you read in each line, you should convert every string to an int. That ought to solve your problem