I wrote this script with Python 2.7:
name=raw_input("Hi im a PC, who are you?")
print("Hi " + name + " how are you, are you good?")
answer = raw_input("")
if (answer) == yes:
print("That's good to hear")
elif (answer) == no:
print("Oh well")
else:
print("Sorry, you didnt answer the question properly, Please answer with a yes or no")
This is the error I get:
Traceback (most recent call last): File "C:/Python27/programs/2", line 4, in if (answer) == yes: NameError: name 'yes' is not defined
you have no variable named yes
,
What you are trying to do is compare the user input to the string "yes"
which would go like:
if answer == "yes":
# do stuff
There is also no need for those brackets around answer.
answer
是一个字符串,您应该使用answer == "yes"
You need to put "
or '
in your strings. yes
is not "yes"
, and no
is not "no"
.
#if (answer) == yes:
if (answer) == "yes":
#elif (answer) == no:
elif (answer) == "no":
The error name 'yes' is not defined
is because the interpreter looked for a variable called yes
because the word is without "
or '
. If you write "yes"
the interpreter will compare the value of answer
variable with the string "yes"
.
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.