popeonhabbo123
popeonhabbo123

Reputation: 71

Can Someone please explain this Code to me

inputArray=[5, 1, 2, 3, 1, 4]
product = -1000
f = 0


for f in range(len(inputArray)):
    try:
        if product<  inputArray[f] * inputArray[f+1]:
             product = inputArray[f] * inputArray[f+1]
             print product
    except:
        'no more'
print product

Result: 5,6

why doesn't it keep multiply the rest of the adjacent elements?

Upvotes: 0

Views: 35

Answers (1)

Kai Qing
Kai Qing

Reputation: 18833

If you'd like that as an official answer, the explanation is below:

It does multiply on every iteration. It just doesn't print and redefine product unless product is less than the value of this iteration multiplied by next iteration. so visualize it like so:

-1000 < 5 so print. now the value of product is 5.

5 > 1 * 2 so do nothing.

5 < 2 * 3 so print. the value of product is now 6.

6 > 3 * 1 so do nothing.

6 > 1 * 4 so do nothing.

So you would have printed only 5 and 6.

Upvotes: 1

Related Questions