Search code examples
pythonpalindrome

How to make a Palindrome Calculator in Python


I am currently partway through a school project which requires me to create a python program that can read in two non-negative integers, begin and end, and print out all of the palindromes which occur between begin and end (inclusive). The code we have been given is this:

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.

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

Question: How would I calculate how many palindromes are in the range of the two numbers entered (and which numbers are palindromes)?

Research: I have tried having a look at pages, this one was a good one though I (being new to python) could not make sense of it when I put it into code: how to check for a palindrome using python logic


Solution

  • Thank you everyone, the code I found to be the answer was this:

    begin = int(input('Enter begin: '))
    end = int(input('Enter end: '))
    
    palindromes = palindromes = len([i for i in range(begin, end+1) if str(i) ==     str(i)[::-1]])
      for i in range(begin, end+1):
        if str(i) == str(i)[::-1]:
          print(i,'is a palindrome')
    
    print('There are', palindromes, 'palindrome(s) between', begin, 'and', end)