简体   繁体   English

为什么python告诉我我的yes变量未定义?

[英]why does python tell me that my yes variable not defined?

I wrote this script with Python 2.7: 我用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 , 您没有名为yes变量,
What you are trying to do is compare the user input to the string "yes" 您想要做的是将用户输入与字符串"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" . 您需要在字符串中输入"' yes并非"yes"no并非"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" . 错误name 'yes' is not defined是因为解释器正在查找名为yes的变量,因为单词中没有"' 。如果您输入"yes"则解释器会将answer变量的值与字符串"yes"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Python 告诉我这没有定义? - Why does Python tell me this is not defined? 为什么 python 3 在我的代码中使用 test.json 时告诉我没有这样的文件或目录 - Why does python 3 tell me that there is no such file or directory when I use test.json in my code 如何运行我的 Python 脚本? 为什么命令行告诉我“没有这样的文件或目录”? - How do I run my Python script? Why does the command line tell me "no such file or directory"? 为什么python的os.stat()告诉我我的文件组是空的? - Why does python's os.stat() tell me my file group is empty? 为什么 Python 告诉我在随机抽样之前进行排序? - Why does Python tell me to sort before taking a random sample? 为什么此python代码告诉我权限被拒绝? - Why does this python code tell me permission is denied? 如果我在上面的 python 中定义它,为什么似乎没有定义我的变量 - Why it appears that my variable is not defined if I defined it above in python 为什么我列出素数的简单程序告诉我“builtin_function_or_method”对象不可下标? - Why does my simple program to list primes tell me that "builtin_function_or_method" object is not subscriptable? 为什么scipy.interpolate.interp1d()告诉我我的xnew值超出范围? - Why does scipy.interpolate.interp1d() tell me my xnew value is out of range? 如果输入为“是”,为什么Python突然停止? - Why does Python suddenly stop if the input is Yes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM