+ 1

Whats wrong with my code?

I want to find 2 two digit numbers that when multiplied give a palindrom number(the same forward as backwards). Here's my code: def palinumber(): for i in range(100): for y in range(100): temp = i * y if temp == str(temp)[::-1]: result.append((i, y)) y += 1 else: y += 1 i += 1 return result

18th Jan 2018, 8:31 PM
Aliuakbar
Aliuakbar - avatar
2 Answers
+ 1
You've to initiate first result as an empty array so you can later add things to it. In your if-condition you're comparing an integer with a string, you've to convert one of them to the type of the other. Maybe you can try something like this def palinumber(): result = [] for i in range(100): for y in range(100): temp = i * y if str(temp) == str(temp)[::-1]: result.append((i,y)) y += 1 else: y += 1 i += 1 return result print(palinumber())
18th Jan 2018, 9:02 PM
Jente
+ 2
Thank you I had the result in the original code initialized but the string integer comparison was the problem
18th Jan 2018, 9:40 PM
Aliuakbar
Aliuakbar - avatar