2017-05-17 12 views
0

私は最初の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 

したいが、多分私はすべてが間違っているこのことについてつもりです。私はランダムな文字列を選択した関数の部分を短くしてリストに変換できるように感じました。

を使用でき
+1

'foo = list(random.choice(a_list))'? –

+4

'random.sample(a_list、1)'が私の選択です(サンプルサイズに関係なくリストを返します)。 – ayhan

+0

目的の出力を表示します。 –

答えて

2

foo = [random.choice(a_list)] 
+0

ありがとうございます。これで構文が明白に見えるようになりました。 –

+0

@RobertHubbardは大歓迎です。 GL –

0

私もPythonの3.xのと私の旅を開始し、ここで私はちょうどあなたが立ち往生か何か取得する場合(あなたが参考のためにそれを使用することができます作られた私の迅速なコードですよ):

from random import choice as chc 

def hangman(diff): 
    #Fill in the words for every difficulty type: 
    easy = ['this', 'that'] 
    medium = ['bicycle', 'bathroom'] 
    hard = ['superlongword', 'othersuperlongw'] 
    if diff == 'easy': 
     word = chc(easy) 
    elif diff == 'medium': 
     word = chc(medium) 
    elif diff == 'hard': 
     word = chc(hard) 
    else: 
     raise ValueError('Bad difficulty choosen. Terminating.') 
    shadow = ['_' for item in word] #list which is showing the progress 
    shad_str = '' 
    for item in shadow: 
     shad_str += item 
    print('Choosen word is {} characters long and looks like this:\n{}'.format(len(shad_str), shad_str)) 
    while '_' in shadow: 
     letter = input('Type in next char: ') 
     if len(letter) == 1: #anti-cheat - always have to give 1 char 
      if letter in word: 
       for i in range(len(word)): #makes it work correctly even when the letter shows up more than one time 
        if(letter == word[i]): 
         shadow[i] = letter 
       temp = '' 
       for item in shadow: 
        temp += item 
       print(temp) 
      else: 
       print("This letter isn't in the choosen word! Try again.") 
     else: 
       print('No cheating! Only one character allowed!') 
    else: 
     shad_str = '' 
     for item in shadow: 
      shad_str += item 
     print('The game has ended. The word was "{}".'.format(shad_str)) 

私はチェックして全部が関数で行うことができると確信している(これは単に「簡単」モードの場合である理由です)ので、あなたは「遊び」関数を3回呼び出すことができますどの難易度モードを選択するかによって異なります。
編集:それは必要ではなかった、私はちょうどあなたが3 ifsとの差分を決定することができます気づいた。ユーザーが正しい難易度を選ぶと、ゲームは毎回機能します。

+0

これに感謝します。あなたは私が今日私のバージョンで作業している間、その言葉を反復するのに便利です。 –