テキスト ベースの RPG を作成しようとしていますが、考えられるすべての入力を 1 つの変数に短縮しようとすると、文字列でリストを終了できません。
input_use = ["use ", "use the "]
...
input_press = ["press ", "press the ", input_use]
...
input_interact_button = input_press + "button"
リストを作成する場合は、リストを既存の値に連結します。
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']
よく見てください:
input_interact_button = input_press + "button"
今、input_press
リストです...しかし"button"
、文字列です! リストと文字列を混在させようとしています。plus( +
) 演算子を呼び出すとき、基本的には「リストと文字列を結合する」と言っています。ブレンダーでピーナッツバターとココナッツをブレンドしようとしているようなものです! これを行う必要があります:
input_interact_button = input_press + ["button"]
"button"
これで、要素が 1 つのリスト内に配置されました。だから...今、リストと別のリストを組み合わせています。動作します!