Need Help with Exercise 13.13 of Python Practice

def product(integer):
  prod = 1
  for x in integer:
    prod *= (x-1)
    print prod
    if integer[x] == integer[-1]:
      return prod
  #print prod
product([1, 2, 3])    
#x is the iterator.  it goes up every time an item in prod is multiplied with another item in integer.

#Do not bother splitting a list.  .split() is for making lists out of strings.

#Your function fails on product([1, 2]). It returns 1 when it should return 2.

Why doesn’t it work?


The list in the argument is not an integer, but a list of integers; most would say this is quite another thing. The name could be pluralized and make more sense to the reader. Just saying.

We need you to walk us through the logic just so we can gain an even footing before going forward. The naming aside, it’s difficult to discern why a multiplication function would need a conditional, for one. Why would we need subtract 1 from any given value? What steps are you attempting to perform, here?

You do know that when x is 1 and we subtract 1, it becomes zero. The product will end up being zero. Subtraction (from an unknown factor) is never a good thing when multiplication is involved.


Let’s mess with a module and take it up a notch…

>>> from functools import reduce
>>> def product(x):
    return reduce(lambda a, b: a * b, x)

>>> product([1,2,3])

Have to ask, did you learn anything from this? Don’t answer, though. The answer is ‘No.’

Somewhere behind all this is the code you need to know so this sort of thing can come about. If you remember this and don’t learn any of the rest, you’ll crash and burn at some point. Like I said in another post earlier today (or yesterday), don’t look for the best, look for all the ways, and keep digging for more.

Why did I say flat out, no? Because nothing has been learned about planning an algorithm. What you are doing above is just that, planning an algorithm so we would never wish to dissuade you of that pursuit. Coach us to what ideas are going though your mind and we can help you seed your garden with those ideas, only fertilized. What I showed above is far from the field we are sowing today. It is for another day, after the harvest of this crop.

def product(integer):
  prod = 1
  for x in integer:
    prod *= integer[x-1]
    if integer[x] == integer[-1]:   #tried to check for if x is the last element in the list here
      return prod
product([1, 2, 3])    
#x is the iterator.  it goes up every time an item in prod is multiplied with another item in integer.


The function is working fine. It returns 2 for me.

This is not correct tho. In your code x is not an itterator but a value the array integer holds. If you print(x) in the for loop and change the numbers for integer to for example [1,3,2] you will see that x does not count up or down. instead it will print the value of integer[x]


In a for loop we do not need to track the progress since that is handled for us.

>>> nums = [5, 4, 3, 2, 1]
>>> for x in nums:
    print (x, end=" ")

5 4 3 2 1 

Note that x represents the values in the list.


what does multiple arguments do in the print statement? Eg. print (x, y)


The arguments will be printed on the same line with one space between, no comma.

>>> x, y = 6, 7
>>> print (x, y)
6 7
>>> print (x - y, x + y, x / y, x * y, y / x)
-1 13 0.8571428571428571 42 1.1666666666666667


with integer[x-1] I want to subtract 1 from index, not the value of x. Otherwise I get ‘list index out of range’


We must consider the manner in which we are iterating over the data.

for value in data:


for index in range(len(data)):

or borh,

for index, value in enumerate(data):

One last thing, may we for the remainder of this discussion refer to the object as n rather than integer? It would at least suggest a sample space, and not a single value.

I’m still confused as to why we must do any subtracting, at all. It’s just not needed for our purposes.

>>> def product(n):
    a = 1
    for b in n:
        a *= b
    return a

>>> product(range(1,6))

def product(integer):
  prod = 1
  for x in range(len(integer)):
    y = x+1
    print "prod %s" % prod
    prod *= integer[x]
    print "Index %s" % integer[x]
    print "Increment %s" % y
  print "Final product is %s" % prod+'!'
  return prod
product([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])    
#x is the iterator.  it goes up every time an item in prod is multiplied with another item in integer.

#Product can be a list, but becomes an integer only if you find a way to reference an index value directly.

#A lot of lines were used up inserting the print statements to make it look pretty.  I don't know how to do new lines within string code.  :/

#range len(integer) works better than simply integer, for some reason.  I thought it would simply read the length of the list, but for is a bit weird that way, I guess?

#the y = x+1 isn't necessary, but that's how I get the printed 'increment' to not start from 0, which otherwise feels out of place; especially if it ends at 9 when the final element in list is 10.  

There I did it. Read the comments, if you want?

I figure there’s more ways to solve this


Suffice it to say, you’re not listening; but, that is another discussion. You can go your own way; who are we to object? So be it.