Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python编程:输入变量返回名称错误_Python_Shell_Variables_Input_Nameerror - Fatal编程技术网

Python编程:输入变量返回名称错误

Python编程:输入变量返回名称错误,python,shell,variables,input,nameerror,Python,Shell,Variables,Input,Nameerror,所以我为这个游戏编写了这段代码,使用了用户输入,但它似乎不起作用。这是我的密码 import random print ("Welcome to the hungry dog game.") print ("The aim of this game is to save the hungry dog. How long can you keep it alive for?") true = True false = False aliveordeadlist1 = [true, false

所以我为这个游戏编写了这段代码,使用了用户输入,但它似乎不起作用。这是我的密码

import random

print ("Welcome to the hungry dog game.")
print ("The aim of this game is to save the hungry dog. How long can you keep it alive for?")


true = True
false = False
aliveordeadlist1 = [true, false]
random.choice(aliveordeadlist1)





feed1 = input("Ok so here's your first choice. Are you going to feed your dog now or not?(he doesn't look very hungry...) type in yes or no.")
if feed1 == "no" and aliveordeadlist1 == false:
    print ("Well you were wrong, he was hungry. Your dog died sorry. Better luck next time.")

if feed1 == "yes" and aliveordeadlist1 == false:
    print ("As I told you he wansn't hungry> You overfed him so hed died sorry. Better luck next time.");
if feed1 == "no" and aliveordeadlist1 == true:
    print ("Well done he wasn't hungry. He is still alive (for now...)");
if feed1 == "yes" and aliveordeadlist1 == true:
    print ("Well done he actually was hungry so he would have died if you didn't feed him.He is still alive (for now...)");
当我在shell中运行此代码时,例如,键入yes,它将返回以下错误:

Traceback (most recent call last):
  File "/Users/mac/Documents/hungrydoggame.py", line 16, in <module>
    feed1 = input("Ok so here's your first choice. Are you going to feed your dog now or not?(he doesn't look very hungry...) type in yes or no.")
  File "<string>", line 1, in <module>
NameError: name 'yes' is not defined
回溯(最近一次呼叫最后一次):
文件“/Users/mac/Documents/hungrydoggame.py”,第16行,在
feed1=输入(“好的,这是你的第一选择。你现在是否要喂你的狗?(它看起来不太饿…)输入是或否”)
文件“”,第1行,在
名称错误:未定义名称“是”

我不确定我为什么会收到这个错误,以及我应该如何修复它,有人有什么建议吗?

我没有得到您得到的错误,但是您需要将random.choice调用设置为一个变量,然后在您的if语句中引用它(而不是
aliveordeadlist1
),否则,您会将列表与单个单词“yes”或“no”进行比较,结果总是返回
False

此代码存在一些严重问题,我认为在某些版本的Python中可能会导致意外行为:

  • 缩进在Python中是相关的。如果您真的在第一行之后缩进了所有代码,我甚至不理解为什么您的代码会运行
  • 您可以为
    True
    False
    定义别名。这可能不是问题,但我不知道
    true
    false
    是否为保留字
还有一个合乎逻辑的问题:

  • 您没有将
    random保存。choice
    在任何位置返回值。您正在有效地检查
    [True,False]==True
    (和
    ==False
其他可能的问题:

  • 您尚未指定正在使用的Python解释器
    print
    是Python 2中的一个函数,但不是Python 2中的函数。我知道
    assert(whatever)
    assert whatever
    至少在Python 2的某些版本中都能工作,但我不确定
    print()
  • 在Python中,分号不用于分隔语句,换行符是

首先,如果您正在执行此处提到的python2.7程序,则应将其用作
输入_raw
,这样它将以字符串形式返回响应,并在第7行中对狗是死是活进行随机选择,但决不将其保存在变量中,您将在该变量中检查
aliveordead=random.choice(aliveordeadlist1)
。这是正确的代码

import random

print ("Welcome to the hungry dog game.")
print ("The aim of this game is to save the hungry dog. How long can you keep it alive for?")


true = True
false = False
aliveordeadlist1 = [true, false]
aliveordead = random.choice(aliveordeadlist1)





feed1 = raw_input("Ok so here's your first choice. Are you going to feed your dog now or not?(he doesn't look very hungry...) type in yes or no.")

if feed1 == "no" and aliveordead == false:
    print ("Well you were wrong, he was hungry. Your dog died sorry. Better luck next time.")

if feed1 == "yes" and aliveordead == false:
    print ("As I told you he wansn't hungry> You overfed him so hed died sorry. Better luck next time.");
if feed1 == "no" and aliveordead == true:
    print ("Well done he wasn't hungry. He is still alive (for now...)");
if feed1 == "yes" and aliveordead == true:
    print ("Well done he actually was hungry so he would have died if you didn't feed him.He is still alive (for now...)");

适合我。使用
raw\u input()