12 Comments
User's avatar
Patricia Toffey's avatar

print("The sum of the numbers is: " + str(calculate_sum(numbers_list)))

Expand full comment
Ardit Sulce's avatar

Correct!

Expand full comment
Navya's avatar

def sum_list(n):

total = 0

for i in n:

total += i

return total

n = list(map(int, input("Enter numbers separated by commas: ").split(",")))

print("The sum is", str(sum_list(n)))

Expand full comment
Yura's avatar

you need to convert the result of the function into a string:

print("The sum of the numbers is: " + str(calculate_sum(numbers_list)))

Expand full comment
Carl Wagner's avatar

You cannot concatenate a string with an integer in Python.

Change last line to::

print("The sum of the numbers is: " + str(calculate_sum(numbers_list)))

Expand full comment
Ardit Sulce's avatar

That would fix it! 👍

Another (good) alternative would be to use an f-string and avoid using "+".

Expand full comment
Tantaluz's avatar

Not sure. But since the function returns an integer we need to change the type of what was returned to string?

Expand full comment
Ardit Sulce's avatar

Yes, using an str() function.

Expand full comment
Wasse's avatar

Because you use “+” sign on print function instead of “,”

Expand full comment
Ardit Sulce's avatar

"+" can also be used inside a print() function, but "," would also be a nice hack here. Best would be to use an f-string.

Expand full comment
Sidharth's avatar

def cal_sum(my_list):

sum = 0

for items in my_list:

sum = sum + items

return sum

my_list = [10,20,30]

total_sum = cal_sum(my_list)

print(f"total sum of list integers is {total_sum})

Expand full comment
VINOD VIJAYAN's avatar

The error is caused by trying to concatenate a string with an integer. The 'calculate_sum(numbers_list)' function returns an integer.

Solution: print("The sum of the numbers is: " + str(calculate_sum(numbers_list))) or

use an f-string

print(f"The sum of the numbers is: {calculate_sum(numbers_list)}")

Expand full comment