13/15


#1

H! I’ve seen a right code for the problem on GitHub.


def product(x):
  n=1
  for i in x:
    n*=i
  return n

As I understand this: i have a list, then I take every value from the list and multiply them by n which in this case is equal to 1. Then I return n.

So if the list is for example: [1,5,7] I will return 1,5,7 which doesn’t make much sense…

What’s the idea of the code?


#2

it will not, how did you come to this conclusion?


#3

1 by 1, 5 by 1. 7*1.
Probably i don’t get the logic


#4

maybe insert some print statements then to see the logic:


def product(x):
  n=1
  for i in x:
    print n
    n*=i
  return n

product([1, 5, 7])

#5

Why do we need n=1 variable?


#6

cause we need a starting value for all the multiplications.


#7

If I multiply 1 by n, I will get n. Sorry I can’t get it:(


#8

n = 1… n holds the value of 1. So you multiply n (which holds 1) with i (which is the iterator variable)


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.