0

I have a simple for loop which loops through the contents of an int list and multiplies every non zero int. I store this value in the variable mult. However, mult is not changing. What's wrong?

def answer(l):
    mult = 0
    for i in l:
        if i != 0:
            if mult == 0:
                mult *= i
    return mult

print (answer([3,4,5]))
4
  • 1
    "max is not changing" ... What is "max" here?? Commented Oct 9, 2018 at 16:46
  • 6
    if mult == 0 - you'll multiply by zero - is that what you want? Commented Oct 9, 2018 at 16:46
  • See how to create a minimal reproducible example Commented Oct 9, 2018 at 16:50
  • should replace first mult = 0 with mult = 1. should remove 2nd if statement. Commented Oct 9, 2018 at 16:57

6 Answers 6

3

If you are setting mult to 0 then mult *=anything will always be 0. Try giving it an initial value of 1

Sign up to request clarification or add additional context in comments.

Comments

2

The error probe statement in your code is mult = 0 that end results with 0 for any input.... Change that statement with mult = 1

def answer(l):
    mult = 1
    for i in l:
        if i != 0:
            mult *= i
    return mult

print (answer([3,4,5]))

Output : 60

2 Comments

shouldnt we initialize mult to 1 not 0?
Yes sir @Stephen Quan. That's what i did, if there is still any issue in code please let me know
2

As @yourPublicDisplayName already pointed out, anything times 0 is 0. You're used to using 0 from running sums. However, this is a running product. For a sum, you begin with the additive identity element, 0; for products, you have to start with the multiplicative identity element, 1.

Also, do not check to see whether your variable is still at the starting value (mult == 0): this would simply stall your progress at one element: once you change that value, you won't include any further non-zero numbers. More simply:

def answer(l):
    mult = 1
    for i in l:
        if i != 0:
            mult *= i
    return mult

print (answer([3,4,5]))

Output:

60

Comments

1

As far as I understood, you are trying to figure out why the variable mult is not changing. It is declared as 0, which stays alway the same with any computations. You might change it to any other values than 0.

Your code must be like:

def answer(l):
    mult = a # a as given value
    for i in l:
        if i != 0:
            if mult == a:
                mult *= i
    return mult

print (answer([3,4,5]))    

Comments

0

Two changes in above code. 1. First mult variable should be =1 2. if mult == 1 this condition will not change your value, after first multiplication condition will never satisfied if first value is not 1

Modification in above

>>> def answer(l):
...     mult = 1
 ..     for i in l:
...         if i != 0:
...             mult *= i
...     return mult
...
>>> print (answer([3,4,5]))

Comments

0

Ofcourse 0 multiplies with anything would be zero. Below is the shorter version of what you are trying to do using accumulate

from operator import add, mul
from toolz.itertoolz import accumulate
import numpy as np
a=np.array([3,4,5])
list(accumulate(mul, a[np.nonzero(a)]))[-1] #60

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.