I think this is something to do with using the input() function? I read other answers that suggest its because you are trying to redefine str but as you can see Im not doing this here. I know there is a better way of achieving the goal of this task, but i'm just using it to explore python not looking to be optimal.
#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
Error Message is as follows
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 variable input
becomes a str
due to input = str(input())
, and when you try to call it later down the line on answer = str(input())
, Python thinks you are trying to call that string input
.
With input = str(input())
you assign a string to the variable input
and you hide the function input
. Then when you call input()
it is a string object and is not callable.
Your problem is a scope
and Variable Shadowing problem.
In short:
When you try to answer = str(input())
python thinks that input
is the variable you defined up there input = str(input())
while you intend the input()
function.
To fix your problem:
change input = str(input())
to be something like my_input = str(input())
so you won't have this problem of shadowing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.