Not sure why while loop is not iterating

import time
start_time = time.time()
product = 0
num1 = 100
num2 = 100
while num1 <= 999:
    while num2 <= 999:
        product = num1*num2
        print(product, num1, num2)
        product = str(product)
        if product == product[::-1]:
            print("*****", product, num1, num2)
            
        num2+=1
num1+=1

print("Process finished --- %s seconds ---" % (time.time() - start_time))

I am trying to find the largest palindrome that can be found from multiplying 2 3-digit numbers

For some reason, num2 iterates as usual, but num1 stays at 100

I don’t get why this is

I’m not a Python guy, but are you sure that num1+=1 is where you want it, in terms of indenting?

2 Likes

Your identation is wrong - num1 is getting incremented AFTER the while-loop is finished.

Thanks but even after I fix the indenting problem, num1 still doesn’t iterate

import time
start_time = time.time()
product = 0
num1 = 100
num2 = 100
while num1 <= 999:
    while num2 <= 999:
        product = num1*num2
        print(product, num1, num2)
        product = str(product)
        if product == product[::-1]:
            print("*****", product, num1, num2)
            
        num2+=1
    num1+=1

print("Process finished --- %s seconds ---" % (time.time() - start_time))

Yes, it is indexing, but your log statement is buried behind logic so you aren’t seeing it. If you log like this:

while num1 <= 999:
    print("***** num1", num1)
    while num2 <= 999:
        print("***** num2", num2)

You can see that it is indexing and maybe you can see why you aren’t getting the results you want.

You could fix (what I suspect is) your issue by moving something around, but is there a reason why you are using a while loop instead of a for loop?

1 Like

Well technically it DOES iterate - as if you look at the value after the program is run.
The problem is, you only increase num1 after num2 is 1000. And you program is not doing anything of substance, if num2 is over 999.

You need to resett the value of num2.
Or using for-loops seems more reasonable.

if product == product[::-1]:

Ehm, what is that supposed to do?

Its supposed to check if the number is a palindrome

Thanks for your help. When I moved the num2 initialization to inside the first while loop it started working

my code now:

import time
start_time = time.time()
product = 0
num1 = 100
palindromes = list()

while num1 <= 999:
    num2 = 100
    while num2 <= 999:
        product = num1*num2
        
        product = str(product)
        if product == product[::-1]:
            palindromes.append(product)
        num2+=1
    num1+=1

print("Process finished --- %s seconds ---" % (time.time() - start_time))
max = sorted(palindromes)[-1]
print(max)

One more question: why is the max becoming 99999 while when I print out the array (without sorting) I see a number that is bigger (906609)?

Because you are using strings, not numbers. And in alphabetical order “zoo” comes after “zigzag”, despite length.

1 Like

Thanks! It works now

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