13/15 Product


#1

This is my code:

def product(intList):
  total = 1
  for i in intList:
    total = total * i
  return total

It doesn’t work and I get an indentation error on line 3. However, in the Code Academy answers:

def product(list):
  total = 1
  for num in list:
    total = total * num
  return total

It seems like almost exactly the same code, just with different names, but it works! Why is this? Does it have to do something with list? I noticed that in the Code Academy answers, list has white text, whereas my variable name was red, just like any other. Does that have something to do with it?

All help is appreciated, thank you!


#2

Your code works fine, with no errors. Not sure where the indentation error is, either. If you get this error, manually remove all indentation and insert four spaces for each block level. (No, there is nothing wrong with two spaces, as we can see. The industry norm is four.)


#3

… can’t find the IndentationError either. It appears that the indentation of the posted code is composed entirely of spaces, with no tabs, so it’s not the same problem that occurs when a user uses a mixture of spaces and tabs for indentation.

@eliteorigin, your code is better than the Codecademy solution that you posted, in that they used the name list for a variable. That’s not a good practice, as there is also a built-in function of the same name. Using the name of a built-in function for the name of a variable renders the function unavailable within the scope of the variable.


#4

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