有没有更有效的方法在 python 中编写一个很长的 "if elif" 链?
Is there a more efficient way to write a really long "if elif" chain in python?
我是真正的编程语言的初学者,我正在使用 Python 编写一个长的 if elif 链来检查 x 和 y 变量作为坐标,并根据坐标执行不同的操作。 (这是基于文本的冒险游戏)
这就是我现在所拥有的,我想知道如何才能以更好的方式做到这一点。
if x == 1 and y == 1:
dothis()
elif x == 1 and y == 2:
dothat()
elif x == 1 and y == 3:
doANewThing()
elif x == 1 and y == 3:
doSomethingDif()
除此之外出现了 900 多次。我讨厌它。请帮助。
我猜测每个案例代表游戏中的不同房间,并且您想为每个房间调用不同的函数。
一个好的模式是将您的数据与代码分开。将每个房间的坐标和功能视为 data,将它们提取到某种房间列表中。 代码你只需要检查玩家的坐标,找到相应的房间函数,然后调用它。
数据
字典非常适合查东西。让我们将 x,y 坐标视为键,将函数视为值。
rooms = {
(1, 1): dothis,
(1, 2): dothat,
(1, 3): doANewThing,
(1, 4): doSomethingDif,
# etc.
}
注意函数名后面没有括号的原因。是 dothis
和 dothat
,而不是 dothis()
和 dothat()
。我们在这里只列出函数的名称;我们实际上并没有给他们打电话。
代码
try:
# Look up the function for the current room...
room_function = rooms[(x, y)]
# ...And call it.
room_function()
# Error! Didn't find an entry for (x, y).
except KeyError:
handle_error()
这有什么好处是您可以轻松地将新房间添加到字典中,而无需编写任何新的 if-else 块。事实上, 没有 if-else 块!
根据您的简短描述,您遇到的问题可能与您认为的不同 =)。
例如,如果我正在编写一个程序来处理国际象棋游戏的第一步,我就不会在函数 move(x, y, new_x, new_y)
.
中写出所有 4096 个选项
尝试查看字典或矩阵(列表的列表)并转向 'lookup -> object action' 模型。例如:
class Thing(object):
DESCRIPTION = 'undescribable beauty'
def describe(self):
return self.DESCRIPTION
class Wall(Thing):
DESCRIPTION = 'a plain old wall'
class Townsfolk(Thing):
DESCRIPTION = 'an angry ten-year-old'
class Nothing(Thing):
DESCRIPTION = 'only the darkness of your soul'
map = {} # coordinate -> thing
# TODO: load this from ascii art
map[(0, 0)] = Wall()
map[(0, 1)] = Townsfolk()
map[(0, 2)] = Wall()
def look_at(pos):
item = map.get(pos, Nothing())
print("You see %s" % item.describe())
>>> look_at((0,0))
You see a plain old wall
>>> look_at((0,3))
You see only the darkness of your soul
我是真正的编程语言的初学者,我正在使用 Python 编写一个长的 if elif 链来检查 x 和 y 变量作为坐标,并根据坐标执行不同的操作。 (这是基于文本的冒险游戏)
这就是我现在所拥有的,我想知道如何才能以更好的方式做到这一点。
if x == 1 and y == 1:
dothis()
elif x == 1 and y == 2:
dothat()
elif x == 1 and y == 3:
doANewThing()
elif x == 1 and y == 3:
doSomethingDif()
除此之外出现了 900 多次。我讨厌它。请帮助。
我猜测每个案例代表游戏中的不同房间,并且您想为每个房间调用不同的函数。
一个好的模式是将您的数据与代码分开。将每个房间的坐标和功能视为 data,将它们提取到某种房间列表中。 代码你只需要检查玩家的坐标,找到相应的房间函数,然后调用它。
数据
字典非常适合查东西。让我们将 x,y 坐标视为键,将函数视为值。
rooms = {
(1, 1): dothis,
(1, 2): dothat,
(1, 3): doANewThing,
(1, 4): doSomethingDif,
# etc.
}
注意函数名后面没有括号的原因。是 dothis
和 dothat
,而不是 dothis()
和 dothat()
。我们在这里只列出函数的名称;我们实际上并没有给他们打电话。
代码
try:
# Look up the function for the current room...
room_function = rooms[(x, y)]
# ...And call it.
room_function()
# Error! Didn't find an entry for (x, y).
except KeyError:
handle_error()
这有什么好处是您可以轻松地将新房间添加到字典中,而无需编写任何新的 if-else 块。事实上, 没有 if-else 块!
根据您的简短描述,您遇到的问题可能与您认为的不同 =)。
例如,如果我正在编写一个程序来处理国际象棋游戏的第一步,我就不会在函数 move(x, y, new_x, new_y)
.
尝试查看字典或矩阵(列表的列表)并转向 'lookup -> object action' 模型。例如:
class Thing(object):
DESCRIPTION = 'undescribable beauty'
def describe(self):
return self.DESCRIPTION
class Wall(Thing):
DESCRIPTION = 'a plain old wall'
class Townsfolk(Thing):
DESCRIPTION = 'an angry ten-year-old'
class Nothing(Thing):
DESCRIPTION = 'only the darkness of your soul'
map = {} # coordinate -> thing
# TODO: load this from ascii art
map[(0, 0)] = Wall()
map[(0, 1)] = Townsfolk()
map[(0, 2)] = Wall()
def look_at(pos):
item = map.get(pos, Nothing())
print("You see %s" % item.describe())
>>> look_at((0,0))
You see a plain old wall
>>> look_at((0,3))
You see only the darkness of your soul