1

タイトルの概要として、文字列のリスト変数を定義しました。リストの内容を入力行の一部として出力する必要があります。これには他のテキストも含まれます。リストの内容を画面に出力する必要があります。角かっこ、ただしアポストロフィなし。

これが私のコードです:

interactive_options = ['list', 'heroes', 'villains', 'search', 'reset', 'add', 'remove', 'high', 'battle', 'health', 'quit']
user_choice = input(f'''
Please enter a choice \n{interactive_options}
''')

現在の出力は次のとおりです。

選択肢を入力してください

['list', 'heroes', 'villains', 'search', 'reset', 'add', 'remove', 'high', 'battle', 'health', 'quit']

...一方、必要なもの:

選択肢を入力してください

[リスト、ヒーロー、ヴィラン、検索、リセット、追加、削除、ハイ、バトル、ヘルス、終了]:

注-リストの内容の最後にコロンを印刷する必要もありますが、これも機能しません。

4

2 に答える 2

1

- を使用している場合、次print(interactive_options)の結果が得られますstr(interactive_options)

>>> print(interactive_options)
['list', 'heroes', 'villains', 'search', 'reset', 'add', 'remove', 'high', 'battle', 'health', 'quit']
>>> str(interactive_options)
['list', 'heroes', 'villains', 'search', 'reset', 'add', 'remove', 'high', 'battle', 'health', 'quit']

ただし、次のようjoinに、(文字列セパレーターで区切られた iterable (リスト、文字列、タプル) のすべての要素を結合して文字列を返す) を使用して、出力を希望どおりにフォーマットすることができます。

>>> ", ".join(interactive_options)
list, heroes, villains, search, reset, add, remove, high, battle, health, quit

次に、ブラケットとコロンを出力に追加できます。

>>> interactive_options_print = ", ".join(interactive_options)
>>> interactive_options_print = "[" + interactive_options_print + "]:"
>>> interactive_options_print
[list, heroes, villains, search, reset, add, remove, high, battle, health, quit]:
于 2022-02-15T22:45:04.193 に答える
-1

あなたはこれを試すことができます -

interactive_options = ['list', 'heroes', 'villains', 'search', 'reset', 'add', 'remove', 'high', 'battle', 'health', 'quit']
user_choice = input(f'''
Please enter a choice \n{str(interactive_options).replace("'","")}:
''')
于 2022-02-15T22:46:01.997 に答える