Global name is not defined

def product(number):
  total = 1
  if num in number:
    total *= num
  return total

print product([1, 2, 3, 4])

Traceback (most recent call last):
  File "python", line 7, in <module>
  File "python", line 3, in product
NameError: global name 'num' is not defined

What am i doing wrong?



if num in number:

why use if? for makes more sense, you want to loop over the numbers in number, right?


oh a typo. have been trying for a hour.

Thank you.


It also explains the error, given when you use a for loop num gets defined in the for loop, where as this is not the case with if, throwing a name error.


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