我正在使用python 3创建一个文本游戏,我需要使用多个elifs,如下所示:
VARIABLE = input ("Prompt Here")
if VARIABLE == ("Possible Answer 1"):
print ("Reward Text")
elif VARIABLE == ("Possible Answer 2"):
print ("Reward Text")
elif VARIABLE == ("Possible Answer 3"):
print ("Reward Text")
答案 0 :(得分:5)
简短回答:是的。
但是,在这样的最小示例中,您可以考虑使用较少的条件来实现此目的的替代方法,例如:
results = { "Possible Answer 1":"Game response 1",
"Possible Answer 2":"Game response 2",
"Possible Answer 3":"Game response 3" }
if VARIABLE in results:
print (results[VARIABLE])
# else :
# default action, if appliable
养成保持执行路径简单并尽可能将数据与行为分开的好习惯!