0

I wrote this code:

def myFunction(*array):
  i = 0
  j = 0
  while i < len(array):
    j = j + array[i]
    i = i + 1
  return j

array=[1,2,3,4]

myFunction(array)

but I got this error:

j = j + array[i]

TypeError: unsupported operand type(s) for +: 'int' and 'list'

I looked for a solution and found one on TypeError: unsupported operand type(s) for +=: 'int' and 'list'

If I change j = j + array[i] to j += sum(array[i]), there is no error and I get a value.

Why did my error happen? Is array[i] a 2D array? Why I must use the sum function?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 3
    In your own words, where you have `def myFunction(*array):`, what does the `*` mean? If you call, for example, `myFunction(1, 2, 3, 4)`, what do you expect to happen? If you call `myFunction([1, 2, 3, 4])`, do you see why that would work differently? What about `myFunction(*[1, 2, 3, 4])` - have you seen things like this? – Karl Knechtel Jan 19 '22 at 11:29
  • Why is there a * before array in arguments? – matszwecja Jan 19 '22 at 11:30
  • Welcome to Stack Overflow. Please read [ask] and note that this is not a discussion forum; we don't want conversational language in questions, and your level of experience is not relevant to asking the question. (Also: in Python, we call them *lists*. Unless you are actually using the standard library `array` module, Numpy, etc.) – Karl Knechtel Jan 19 '22 at 11:30
  • Change function signature: `def myFunction(array):` and it will work as expected. – sagi Jan 19 '22 at 11:30
  • @sagi is correct. See https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters for more information. – Tom Aarsen Jan 19 '22 at 11:31
  • There are two ways to fix the problem, either change function signature from `def myFunction(*array):` to `def myFunction(array):`, or change function call syntax from `myFunction(array)` to `myFunction(*array)`. Using `*` in function signature means that you expect variable amount of positional arguments, hence you have to call it with `myFunction(*array)` or something like `myFunction(1, 2, 3, 4)`. – Arty Jan 19 '22 at 11:33
  • @KarlKnechtel I learn about *args in w3schools on the python function section. At first, I thought I need to add * to the arguments so the code could work. Ah I see, it's different between list and array. I should use *args if I want to pass a list into my function. Thank you Karl for your explanation! – Rizky Wijayanto Jan 19 '22 at 12:47
  • @matszwecja I thin because I failed to understand how *args work before. – Rizky Wijayanto Jan 19 '22 at 12:49
  • @sagi yeah that's solved the problem thanks! – Rizky Wijayanto Jan 19 '22 at 12:50
  • @Arty got it, thanks for your explanation Arty! – Rizky Wijayanto Jan 19 '22 at 12:51
  • You should read https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters instead to understand *args. – Karl Knechtel Jan 19 '22 at 22:48

1 Answers1

0

this will work for you:

def myFunction(array):
  i = 0
  j = 0
  while i < len(array):
    j = j + array[i]
    i = i + 1
  return j

array=[1,2,3,4]

myFunction(array)
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21