gpt4 book ai didi

Python 3.7 字典给出错误的值作为输出

转载 作者:行者123 更新时间:2023-12-01 07:15:06 25 4
gpt4 key购买 nike

我正在用 Python 创建一个基于文本的游戏(我知道,这是一个非常常见的初学者项目),我在嵌套字典中遇到了一个问题。这是字典代码:

gamemap = {
"a1": {
ZONENAME : "Town Market",
DESCRIPTION : "This is the area where most goods are bought and sold",
EXAMINATION : "There are 3 people in this area. You can talk to them.",
SOLVED : False,
UP : "",
DOWN : "b1",
LEFT : "",
RIGHT : "a2",
KEY : "pizzeria"

},
"a2": {
ZONENAME : "Town Entrance",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "",
DOWN : "b2",
LEFT : "a1",
RIGHT : "a3"
},
"a3": {
ZONENAME : "Town Square",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "",
DOWN : "b3",
LEFT : "a2",
RIGHT : "a4"
},
"a4": {
ZONENAME : "Town Hall",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "",
DOWN : "b4",
LEFT : "a3",
RIGHT : ""
},
"b1": {
ZONENAME : "School",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "a1",
DOWN : "c1",
LEFT : "",
RIGHT : "b2"
},
"b2": {
ZONENAME : "Home",
DESCRIPTION : "This is where you live. It is your home.",
EXAMINATION : "Your house is a bright pink colour. I'm not judging you, but... ok I'm judging you. I'm sorry.",
SOLVED : False,
UP : "a2",
DOWN : "c2",
LEFT : "b1",
RIGHT : "b3"
},
"b3": {
ZONENAME : "",
DESCRIPTION : "",
EXAMINATION : "",
SOLVED : False,
UP : "a3",
DOWN : "c3",
LEFT : "b2",
RIGHT : "b4"
},
"b4": {
ZONENAME : "",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "a4",
DOWN : "c4",
LEFT : "b3",
RIGHT : ""
},
"c1": {
ZONENAME : "",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "b1",
DOWN : "d1",
LEFT : "",
RIGHT : "c2"
},
"c2": {
ZONENAME : "",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "b2",
DOWN : "d2",
LEFT : "c1",
RIGHT : "c3"
},
"c3": {
ZONENAME : "",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "b3",
DOWN : "d3",
LEFT : "c2",
RIGHT : "c4"
},
"c4": {
ZONENAME : "",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "b4",
DOWN : "d4",
LEFT : "c3",
RIGHT : ""
},
"d1": {
ZONENAME : "",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "c1",
DOWN : "",
LEFT : "",
RIGHT : "d2"
},
"d2": {
ZONENAME : "",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "c2",
DOWN : "",
LEFT : "d1",
RIGHT : "d3"
},
"d3": {
ZONENAME : "",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "c3",
DOWN : "",
LEFT : "d2",
RIGHT : "d4"
},
"d4": {
ZONENAME : "",
DESCRIPTION : "description",
EXAMINATION : "examine",
SOLVED : False,
UP : "c4",
DOWN : "",
LEFT : "d3",
RIGHT : ""
}

}

看起来不错,对吧?看看这个:

def print_location():
current_place = "You are in the " + gamemap[myPlayer.location][ZONENAME]
for character in current_place:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.03)

正如你所看到的,这个函数告诉玩家他们在哪里,打印 ZONENAME 变量。这对他们中的大多数人都有效。例如,如果我转到a2,它会打印“You are in the Town Entrance”。

但是,如果我转到 a1,即使该函数中的代码相同,它也不会打印:“You are in the Town Market”(这是该区域的 ZONENAME),而是打印“You are in比萨店”。 “pizzeria”是a1的KEY值,而不是zonename。

奇怪的是,如果我注释掉或删除 key ,它会打印正确的内容:“你在城镇市场”。

我尝试在其他嵌套字典上添加 KEY 值,但问题仍然存在。一旦我添加该变量,它就会打印该变量。这很令人困惑。我也尝试过更改 KEY 的名称,但也不起作用。

请帮忙解决这个问题。如果您需要查看更多脚本,我可以提供。提前致谢。

最佳答案

我相信您的问题在于您的词典定义。为了节省空间,我简化了您的示例。

ZONENAME="ZONENAME"
KEY="ZONENAME"
# ...
gamemap = {
"a1": {
ZONENAME : "Town Market",
KEY : "pizzeria"
},
"a2": {
ZONENAME : "Town Entrance",
},
}

对于 ZONENAME、LEFT 等,您使用变量名称而不是字符串文字。现在这些可能是在您的程序中的某个地方定义的,因此程序仍然可以正常运行,但它具有误导性。我愿意打赌变量 KEY 在字典定义之前的某个位置被定义为“ZONENAME”。

程序中的实际情况是:

gamemap = {
"a1": {
"ZONENAME" : "Town Market",
"ZONENAME" : "pizzeria" # Overwrites Town Market key
},
"a2": {
"ZONENAME" : "Town Entrance",
},
}

将变量名称用单引号或双引号引起来,我相信您会从该函数中获得预期的行为,但这可能会破坏程序中的其他内容。

gamemap = {
"a1": {
"ZONENAME" : "Town Market",
"KEY" : "pizzeria"
},
"a2": {
"ZONENAME" : "Town Entrance",
},
# ...
}
def print_location():
current_place = "You are in the " + gamemap[playerLocation][ZONENAME]
print(current_place)

现在当我们运行时

playerLocation = "a1"
ZONENAME = "ZONENAME"
print_location()
# prints: You are in the Town Market

ZONENAME = "KEY"
print_location()
# prints: You are in the Pizzaria

请参阅此链接自行运行代码:https://repl.it/repls/MajesticMeanAutosketch

关于Python 3.7 字典给出错误的值作为输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58017510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com