-2

我正在尝试制作基于文本的 RPG,当我尝试将每个可能的输入缩短为一个变量时,我无法用字符串结束列表:

input_use = ["use ", "use the "]
...
input_press = ["press ", "press the ", input_use]
...
input_interact_button = input_press + "button"
4

2 回答 2

6

如果要构建列表,请将列表连接到现有值:

input_press = ["press ", "press the "] + input_use

input_interact_button = input_press + ["button"]

演示:

>>> input_use = ["use ", "use the "]
>>> input_press = ["press ", "press the "] + input_use
>>> input_interact_button = input_press + ["button"]
>>> input_interact_button
['press ', 'press the ', 'use ', 'use the ', 'button']
于 2013-10-01T17:32:12.333 回答
1

仔细看:

input_interact_button = input_press + "button"

现在,input_press是一个列表......但是"button"是一个字符串!您正在尝试混合使用列表和字符串。当您调用 plus( +) 运算符时,您基本上是在说“将列表与字符串组合”。这就像试图在搅拌机中混合花生酱和椰子!你需要这样做:

input_interact_button = input_press + ["button"]

现在,您放置"button"在一个单元素列表中。所以......现在你正在组合一个列表和另一个列表。作品!

于 2013-10-01T18:15:44.227 回答