-2

テキスト ベースの RPG を作成しようとしていますが、考えられるすべての入力を 1 つの変数に短縮しようとすると、文字列でリストを終了できません。

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"これで、要素が 1 つのリスト内に配置されました。だから...今、リストと別のリストを組み合わせています。動作します!

于 2013-10-01T18:15:44.227 に答える