Search code examples
pythonpalindrome

Palindrome Checker for numbers


I'm currently trying to do an assignment. I need to some help with my palindrome checker. This is what I've done so far

begin = int(input('Enter begin: '))
end = int(input('Enter end: '))

palindromes = 0
#Add your code here. You will want to start with a "for x in range" style loop.
for x in range(begin, end, -1):


print('There are', palindromes, 'palindrome(s) between', begin, 'and', end)

This is not a function. Please help.


Solution

  • The easiest way to check if a number is a palindrom would probably to treat it as a string, flip it, and check if the flipped string is equal to the original. From there on, it's just a loop that counts how many of these you've encounteres:

    begin = int(input('Enter begin: '))
    end = int(input('Enter end: '))
    
    palindromes = 0
    #Add your code here. You will want to start with a "for x in range" style loop.
    for x in range(begin, end):
        if str(x) == str(x)[::-1]:
            palindromes += 1
    
    print('There are', palindromes, 'palindrome(s) between', begin, 'and', end)