Palindrome检查器的数字

时间:2015-08-10 07:17:03

标签: python palindrome

我目前正在尝试做作业。我需要帮助我的回文检查。这是我到目前为止所做的事情

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)

这不是一个功能。请帮忙。

1 个答案:

答案 0 :(得分:1)

检查数字是否为回文的最简单方法可能是将其视为字符串,将其翻转,然后检查翻转的字符串是否与原始字符串相等。从那以后,它只是一个循环,计算你遇到的这些中有多少:

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)