Mistake in the background code in "product"


This is the code I wrote for the exercise:

def product(sequence):
    total = sequence[0]
    for x in sequence:
        if type(x) != str and x != sequence[0]:
            total *= x

This is wrong according to the course program; "Oops, try again. Your function fails on product([6, 6, 6, 6, 6]). It returns 6 when it should return 7776".

I tried to interpret the code in my own pycharm, and I get the correct result. And yes, the indentations are correct.


the problem is in this bit:

x != sequence[0]

in case of [6,6,6,6] sequence[0] is 6, so for every run of the loop you have:

if 6 != 6

which is false, so if will never evaluate to true, so it never gets to total *= x


Thanks for the debug. I feel ashamed :smiley:


Now you can get further. Try to solve this now you know where the problem is, if you need more help, post an updated version of your code


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