Blue Week Special Offer | Brighten your week!
days
hours
minutes
seconds Can i not sum two ints?

Why does my equation for average give me an int’ object is not iterable error?

My Code:

test_data = ["1912", "1929", "1913-1923",
"(1951)", "1994", "1934",
"c. 1915", "1995", "c. 1912",
"(1988)", "2002", "1957-1959",
"c. 1955.", "c. 1970's",
"C. 1990-1999"]

def strip_characters(string):
string = string.replace(char,"")
return string

stripped_test_data = ['1912', '1929', '1913-1923',
'1951', '1994', '1934',
'1915', '1995', '1912',
'1988', '2002', '1957-1959',
'1955', '1970', '1990-1999']

def process_date(string):
if "-" in string:
split_date = string.split("-")
date_one = split_date
date_two = split_date
average = sum(int(date_one),int(date_two))/2
average = round(average)
else:
int(string)
return string

processed_test_data = []
for date in stripped_test_data:
date = process_date(date)
processed_test_data.append(date) ```

What I expected to happen:

For this line: average = sum(int(date_one),int(date_two))/2 , why can’t I use the sum function for the two dates ? It shows me an error ‘int’ object is not iterable. And the answer key just shows date = (int(date_one) + int(date_two)) / 2

Also why can’t we do this instead, converting the date_one and date_two variables to integers before calculating the average?

date_one = int(split_date)
date_two = int(split_date)

What actually happened:

Replace this line with the output/error

<!--Enter other details below: -->

As the error states, int object is not iterable. You can’t iterate through a single integer. sum() is a built-in function that would only work on objects that are iterable - like lists, for example.

You can do that. In both approaches, you are converting them to integers before calculating the average.

Also, for future reference, please make sure to include the link to the mission/lesson screen as well in your post. It helps provide easier access to the necessary context.

1 Like

thank you once again! i’ll put the screen link next time i have a q