【发布时间】:2013-04-30 19:26:37
【问题描述】:
我需要以下帮助:
编写一个函数(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 循环对它们进行计数。我不知道接下来要为(主)函数添加什么以实现“仅当它是回文时才将数字添加到总数中(调用 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()
有谁知道怎么回事?
【问题讨论】:
-
你应该正确缩进这两个函数的内容。
标签: python python-3.x palindrome