我是Python3的新手,在遇到Euler项目问题时遇到了麻烦。请不要给我我想了解为什么这是错误的代码。顺便说一句,回文是一个以相同的方式向后读取的数字,例如98089或6759576。
我不确定该怎么办,我认为是Holder变量不能作为整数工作。
numberString = 0
holder = 0
partOne = 0
partTwo = 0
counter = 0
num = 0
def isPalindrome(number):
if number % 2 == 0: #EVEN
numberString = str(number)
holder = len(numberString)
holder = holder // 2
holder = int(holder)
partTwo = numberString[-(holder),-1]
partOne = numberString[0,(holder - 1)]
if partOne == partTwo:
return True
else:
return False
elif number % 2 != 0: #ODD
numberString = str(number)
holder = len(numberString) // 2
partTwo = numberString[-(holder),-1]
partOne = numberString[0, (holder - 1)]
if partOne == PartTwo:
return True
else:
return False
for first in range(100,1000):
for second in range(100,1000):
num = first * second
if isPalindrome(num) == True:
print(num,'is a palindrome.')
对于所有可能的3位数字的回文,我希望有一个例子=回文。
我明白了:
Traceback (most recent call last):
File "main.py", line 38, in <module>
if isPalindrome(num) == True:
File "main.py", line 15, in isPalindrome
partTwo = numberString[-(holder),-1]
TypeError: string indices must be integers