My code is written in python 3 and it is meant to print out palindromes. It should iterate through all the palindromic products of 2 3-digit numbers as shown below:
mylist=[]
for i in range(999,99,-1):
for x in range(999, i-1, -1,):
number=i*x
number=str(number)
if number==number[::-1]:
#print(number)
mylist.append(number)
mylist=mylist.sort(reverse=True)
print(mylist)
Note the commented out print. When this was still in place, all the palindromes that should have been printed out came out.
When I run my code, without the print
statement, the console only prints out 'None'.
As far as I can see my logic is in order, so why is this happening? EDIT: Also, when I sort my list in reverse order, 99999 comes first. I gather this is because python looks at the consecutive 9's and thinks it is the biggest. However, is there an easy way to get the actual biggest number?
list.sort()
always returns None (the list is sorted in-place). What you want is sorted(list)
which returns a new sorted list.
Then you can sort them like this:
mylist = [int(x) for x in mylist]
mylist.sort(reverse=True)
mylist = [str(x) for x in mylist]