-1

I am trying solve this problem with Python:

"Find the largest palindrome made from the product of two 3-digit numbers." This is my code:

list_number = reversed(range(10000, 998002))


def check_number(x): 
    for element in reversed(range(100, 1000)):
        while True:
            if x >= 100:
                if element * x == i:
                    print(i)
                    break
                else:
                    x -= 1
                    continue
            else:
                break


for i in list_number:    
    if str(i) == str(i)[::-1]:
        check_number(999)
    else:
        continue

The code works with 2-digit numbers but not with 3-digit numbers. Can someone pleas tell me what is incorrect?

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Mampel
  • 3
  • 1

1 Answers1

0

You code is extremely badly written, but the real problem is in the statement x -= 1: The value of x is decremented at each iteration of the inner loop and at the end of the first inner loop, x is 99. This makes the condition if x >= 100 false for all consequent iterations of the outer loop.

Add x=999 immediately after for element in reversed(range(100, 1000)). Or, better, learn some good programming and rewrite the program entirely.

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • Thank you for your comment, I am still a beginner so I am still learning, I know the code is not perfect but I learn python since a few weeks, I'll try to improve the code a bit! – Mampel Jan 14 '18 at 08:28