1

我需要以下帮助:

编写一个函数(main),它将要求用户输入一个开始和结束的数字范围(包括)。使用 while 循环计算数字。仅当它是回文时才将数字添加到总数中(调用 isNumberPalindrome)。添加数字后打印总数。

到目前为止,我对这个(主要)功能的了解是......

def main():
start = int(input("Enter a number to start counting at:"))
end = int(input("Enter a number to end counting at:"))
while start <= end:
    print(start)
    start = start + 1

这就是我的 (isNumberPalindrome) 函数。

def isNumberPalindrome(s):
if len(s) < 1:
        return True
else:
    if s[0] == s[-1]:
        return isNumberPalindrome(s[1:-1])
    else:
        return False

到目前为止,我的(主)函数要求用户输入(开始和结束数字)并使用 while 循环对它们进行计数。我不知道接下来要为(main)函数添加什么代码以实现“仅当它是回文时才将数字添加到总数中(调用isNumberPalindrome)。添加数字后打印总数。”

谢谢您的帮助。

到目前为止,提供给我的代码就是这样。

Enter a number to start counting at:1
Enter a number to end counting at:6
1
Traceback (most recent call last):
File "C:/Users/Tyler/Desktop/AWDADXC.py", line 38, in <module>
main()
File "C:/Users/Tyler/Desktop/AWDADXC.py", line 33, in main
if isNumberPalindrome(start):
File "C:/Users/Tyler/Desktop/AWDADXC.py", line 18, in isNumberPalindrome
if len(s) < 1:
TypeError: object of type 'int' has no len()

有谁知道出了什么问题?

4

7 回答 7

2

这是一个简单的尝试:

def isNumberPalindrome(n):
    return str(n) == str(n)[::-1]
filter(isNumberPalindrome, range(10,100))

输出:

[11, 22, 33, 44, 55, 66, 77, 88, 99]

总结他们只需将序列传递给sum()这样的:

sum(filter(isNumberPalindrome, range(10,100)))

输出:

495

如果你想考虑用户输入,你可以这样做:

sum(filter(isNumberPalindrome, range(int(raw_input('Enter a starting number:')), int(raw_input('enter a stopping number:')))))

这将产生以下输出:

Enter a starting number:10
enter a stopping number:100
495
于 2013-05-07T00:11:39.530 回答
1

The problem with your code is that you are calling len on an integer. What you should do is call len on an iterable, e.g. list or string (as in the solutions posted in the answers).

For example, and using your recursive implementation of isNumberPalindrome,

>>> isNumberPalindrome("123321")
True
>>> isNumberPalindrome("HelloolleH")
True
>>> isNumberPalindrome(str(123321))
True
>>> isNumberPalindrome([1,2,3,4,3,2,1])
True

Other than that, if it is not necessary to use your recursive implementation, any of the other answers, should do the work.

于 2013-05-07T00:40:44.070 回答
1

遍历从start到包含的所有整数end

for n in range(start, end + 1):
    print(n)

获取数字;你可以使用str(n)

def is_palindrom(number):
    digits = str(number)
    return digits == digits[::-1]

where反向s[::-1]返回字符串,例如.s"abc"[::-1] == "cba"

求 [start, end] 范围内所有回文的总和:

total = sum(number for number in range(start, end + 1) if is_palindrom(number))
于 2013-05-07T00:15:44.540 回答
0

您需要有一个可以存储回文总和的变量。不仅仅是调用 if 语句来检查数字是否是回文,如果是,则将数字添加到总数中,否则什么也不做。

PS:您可以使用另一个答案中提供的 Juampi 代码。

于 2013-05-07T00:06:33.890 回答
0

您的问题是假设参数 toisNumberPalindrome是 a str,但您传递的是int

检查回文很容易,而无需转换为str. 例如。

def isNumberPalindrome(n):  # n is an integer
    s = n
    t = 0
    while s:
        t = t*10 + s%10
        s /= 10
    return n == t
于 2013-05-07T03:24:47.680 回答
0
def main():
start = int(input("Enter a number to start counting at:"))
end = int(input("Enter a number to end counting at:"))
total = 0
while start <= end:
    print(start)
    if isNumberPalindrome(str(start)):
        total += start
    start = start + 1
print(total)
于 2013-05-06T23:40:14.747 回答
0

JavaScript - 解决方案。

用输入数字指定范围


function findPalindromes(number) {

    let palindromeCount = 0;
    let leftSide = '';
    let rightSide = '';

    for (let i = 1; i <= number; i++) {

        let checkBox = [];
        let s = i.toString();

        for (let j = 0; j < s.length / 2; j++) {
            leftSide = s[j];
            rightSide = s[s.length - 1 - j];

            if (rightSide === leftSide) {
                checkBox.push('y');
            } else {
                checkBox.push('n');
            }
        }

        if (!checkBox.includes('n') && i > 10) {
            console.log(s);
            palindromeCount++;
        }
    }

    console.log(`TOTAL: ${palindromeCount} palindromes found`);
}

findPalindromes(1001);

查看结果

于 2018-12-05T07:55:16.093 回答