Global name is not defined


#1
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?


#2

here:

if num in number:

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


#3

oh a typo. have been trying for a hour.

Thank you.


#4

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.


#5

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