title: Convert a loop "for" into a function
body: <p>I'm new to python and I want to convert a loop &quot;for&quot; into a function. My loop I created enables me to multiply all the number of a list and print the result. This is my loop:</p>
  <pre><code>a=[1,2,3,4,9]
  y=1
  for x in a:
     y=y*x
     print(y)
  </code></pre>
  <p>As you can see I tried it with a certain list and I always put y=1 to start my loop and it works very well but when I want to create a function of that loop it fails, so far I tried that:</p>
  <pre><code>a=[]
  y=1
  def produit_entier(a):
     for x in a:
         y=y*x
         return y  
  a=[1,2,3,4,9]
  y=1
  print(produit_entier(a))
  </code></pre>
  <p>As you can see I tried it with a certain list and when I run my code it tells me &quot;local variable 'y' referenced before assignment&quot;, and when I remove the &quot;y=1&quot; line and put it right after the &quot;for x in a:&quot; this message disappear but the result is always 1.</p>
  <p>So I'm lost here, my function is almost exactly the same as my loop, but my loop works very well and not my function, so I would really appreciate help here. Thx in advance</p>