Getting: 'TypeError: can only concatenate tuple (not "int") to tuple', even though it is an int

python, python-3.x

Solution

You are using commas where you shouldn't, creating tuples by accident:

sum_1 = sum_1 + i*3,  # < no comma needed there

Get rid of those commas and your code will work.

A comma creates a tuple in Python:

>>> 2,
(2,)

Problem

``` num_1 = 3 num_2 = 5 num_3 = 15 div_1 = 1000/3 div_2 = 1000/5 div_3 = 1000/15 sum_1 = 0 sum_2 = 0 sum_3 = 0 i = 0 while (i<300): sum_1 = sum_1 + i*3, i = i + 1 print (sum_1) i = 0 while (i<div_2): sum_2 = sum_2 + i*5, i += 1 i = 0 while (i<div_1): sum_3 = sum_3 + i*5, i += 1 print (sum_1) ``` Output: ``` (0,) Traceback (most recent call last): File "C:/Users/xxxxx/Documents/Python/1-p.py", line 12, in <module sum_1 = sum_1 + i*3, TypeError: can only concatenate tuple (not "int") to tuple ``` I am a beginner at Python and I am trying to write a simple code. I am not sure what is going wrong. Can anyone please help me out. I really appreciate it.

Original source