Question

So I need to save the results of a loop and I'm having some difficulty. I want to record my results to a new list, but I get "string index out of range" and other errors. The end goal is to record the products of digits 1-5, 2-6, 3-7 etc, eventually keeping the highest product.

def product_of_digits(number):
        d= str(number)
        for integer in d:
            s = 0
            k = []
            while s < (len(d)):
                j = (int(d[s])*int(d[s+1])*int(d[s+2])*int(d[s+3])*int(d[s+4]))
                s += 1
                k.append(j)
            print(k)

product_of_digits(n)
Was it helpful?

Solution

Similar question some time ago. Hi Chauxvive

This is because you are checking until the last index of d as s and then doing d[s+4] and so on... Instead, you should change your while loop to:

while s < (len(d)-4):

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top