Practice Makes Perfect, 13: Product


This is my code:

def product(n):
product_list = []
total = 1
for number in n:
total = total * number
return total

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

It works but there is one thing that I don't understand.

For this part here:
total = 1
for number in n:
total = total * number

Why does the total have to equal 1 and after that, how does this part of the code actually multiply all the numbers in the list together? I seriously don't understand how it multiplies them together. Can someone explain this fully to me? It would be very much appreciated.


How can you not understand a solution you wrote?

Maybe you can add some print statements so you can see what the code is actually doing

number will contain each value from list (in order/sequence) by multiplying number with total and storing the result in total, total will increase with each multiplication.


I literally just understood it right now, I feel like such an idiot right now :joy: Thanks for your help by the way, I understand that part now.


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