name = raw_input("Welcome soldier. What is your name? ")
print('Ok,', name, ' we need your help.')
print("Do you want to help us? (Yes/No) ")
ans = raw_input().lower()
while True:
ans = raw_input().lower()("This is one of those times when only Yes/No will do!" "\n" "So what will it be? Yes? No?")
ans = raw_input().lower()
if ans() == 'yes' or 'no':
break
if ans == "yes":
print ("Good!")
elif ans == "no":
print("I guess I was wrong about you..." '\n' "Game over.")
When I answer this happens;
First a blank line, then if I press the enter key again;
File "test.py", line 11, in <module>
ans = raw_input().lower()("This is one of these times when only Yes/No will
do!" "\n" "So what will it be? Yes? No?")
TypeError: 'str' object is not callable
What seams to be the problem?
PS I searched the site but it seams that all the people with the same problem had far more advanced scripts and I did not understand anything.
TypeError: 'str' object is not callable
usually means you are using your ()
notation on a string, and Python tries to use that str
object as a function. eg "hello world"()
, or "hello"("world")
I think you meant to do :
ans = raw_input("This is one of those times...").lower()
Another mistake:
if ans() == 'yes' or 'no':
You have to check for both conditions individually,
Should be :
if ans == 'yes' or ans == 'no':
break
or more inline with what you wanted in the first place:
if ans in ('yes', 'no')
The first error is in the line
ans = raw_input().lower()("This is one of those times when only Yes/No will do!"
"\n" "So what will it be? Yes? No?")
The result of lower()
is a string, and parentheses after that mean that the object on the left (the string) gets called. Therefore, you get your error. You want
ans = raw_input("This is one of those times when only Yes/No will do!\n"
"So what will it be? Yes? No?").lower()
Also,
if ans() == 'yes' or 'no':
does not what you expect. Again, ans
is a string, and parentheses mean that the object on the left (the string) gets called. Therefore, you get your error.
Also, or
is a logical operator. Even after removing the parentheses after ans
, the code gets evaluated as:
if (ans == 'yes') or ('no'):
Since a non-empty string ( 'no'
) evaluates to the boolean value True, this expression is always True. You simply want
if ans in ('yes', 'no'):
Additionally, you want to unindent the last lines. All in all, try:
name = raw_input("Welcome soldier. What is your name? ")
print('Ok, ' + name + ' we need your help.')
ans = raw_input("Do you want to help us? (Yes/No)").lower()
while True:
if ans in ('yes', 'no'):
break
print("This is one of those times when only Yes/No will do!\n")
ans = raw_input("So what will it be? Yes? No?").lower()
if ans == "yes":
print("Good!")
elif ans == "no":
print("I guess I was wrong about you..." '\n' "Game over.")
You need to do raw_input("This is one of those times when only Yes/No will do!" "\\n" "So what will it be? Yes? No?").lower()
.
When you do raw_input().lower()
, that already calls raw_input()
and converts the result to lowercase. By that time it's too late to try to pass your prompt string.
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.