私は最初のpythonプロジェクトとしてHangmanのゲームを構築しています。私はそれを構築するように、ゲームは現在ターミナルで実行されています。私はPyGameを使用していません。関連するコードの詳細は次のとおりです。プレイヤーの推測では、(私は文字列を反復処理することができます知っている)リスト内にある場合Pythonで文字列をランダム化してリストにする方法
- チェック:
five_letters = ['birds', 'hands', 'trees'] difficulty = 'easy' def get_word(foo): """Based on difficulty generate a game_word, then turn it into a list to be returned""" if foo == 'easy': foo = random.choice(five_letters) foo = list(foo) if foo == 'medium': foo = random.choice(ten_letters) foo = list(foo) if foo == 'hard': foo = random.choice(fifteen_letters) foo = list(foo) return foo game_word = get_word(difficulty)
私はリストを取り、次の操作を実行します。
- 文字列内に推測がある場合は、出力の適切なスペースに配置します。
文字列をリストとして返すので、出力内の正しい場所に配置するために、リスト内の正しい推測値を見つけることができます。例えば
game_word = 'birds'
player_guess = 's'
私は出力
_ _ _ _ s
したいが、多分私はすべてが間違っているこのことについてつもりです。私はランダムな文字列を選択した関数の部分を短くしてリストに変換できるように感じました。
を使用でき
'foo = list(random.choice(a_list))'? –
'random.sample(a_list、1)'が私の選択です(サンプルサイズに関係なくリストを返します)。 – ayhan
目的の出力を表示します。 –