内的所有回文数我需要一些帮助,下面:查找范围
写一个函数(主),它会要求用户输入开始和结束号码范围(含)。使用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()
有谁知道什么是错的?
您应该正确缩进这两个函数的内容。 – fgb 2013-05-06 23:38:03