我认为这与使用input()函数有关? 我阅读了其他暗示它的答案,因为您正在尝试重新定义str,但是您可以看到Im在这里没有这样做。我知道有一种更好的方法可以实现此任务的目标,但我只是用它来探索python而不是寻求最佳。
#Ask the user for a string and print out whether this string is a palindrome or not.
#(A palindrome is a string that reads the same forwards and backwards.)
repeat = True
while repeat == True:
print(" enter a string " )
input = str(input())
#split string into an array of characters
string = list(input)
print(string)
stringReverse = list(input[::-1])
print(stringReverse)
#loop an check if the first char matches last char up to the halfway
length = len(string)
#get the halfway point
if length%2 == 0:
#halfway point is even
halfway = length/2
else:
halfway=(length+1)/2 #we are going to ignore this character
print(halfway)
print(stringReverse)
#loop through each character in string
currentpost = 0
palindrome=True
for index, letter in enumerate(string):
#print(str(index)+ ' ' + letter)
#print(stringReverse[index])
print( letter +stringReverse[index])
if letter == stringReverse[index]:
print('match')
else:
print('break')
palindrome = False
print(palindrome)
print("continue y/n")
answer = str(input())
print(answer)
if answer =="n":
repeat = False
错误信息如下
Traceback (most recent call last):
File "/Users/nigel/Desktop/pythonshit/exercises6.py", line 48, in <module>
answer = str(input())
TypeError: 'str' object is not callable
It's because you have named your variable
input
on the second line of your code. What happens is that the variableinput
becomes astr
due toinput = str(input())
, and when you try to call it later down the line onanswer = str(input())
, Python thinks you are trying to call that stringinput
.With
input = str(input())
you assign a string to the variableinput
and you hide the functioninput
. Then when you callinput()
it is a string object and is not callable.将会
因为input()函数默认返回str类型