# Hangman・首吊り ###### tags: `Python HomeWork` [wiki](https://ja.wikipedia.org/wiki/%E3%83%8F%E3%83%B3%E3%82%B0%E3%83%9E%E3%83%B3_(%E3%82%B2%E3%83%BC%E3%83%A0)) [web](https://hangmanwordgame.com/?fca=1&success=0#/) ## Step - 1 * TODO-1 - Randomly choose a word from the word_list and assign it to a variable called chosen_word. * TODO-2 - Ask the user to guess a letter and assign their answer to a variable called guess. * TODO-3 - Check if the letter the user guessed (guess) is one of the letters in the chosen_word. :::warning * TODO-1 - word_listからランダムに単語を選び、chosen_wordという変数に代入します。 * TODO-2 - ユーザーに文字を推測してもらい、その答えを guess という変数に代入します。 * TODO-3 - ユーザーが推測した(guess)文字が、choosen_wordの各文字と一致しているかどうかをチェックします。 * 同じ文字であれば、"right"を表示してください。 * 別の文字であれば、"wrong"を表示してください。 ::: **出力例1:** ![](https://i.imgur.com/bym4hjz.png) **出力例2:** ![](https://i.imgur.com/I8HC73K.png) :::info code: ```python= # テストしやすいように、3つの単語を例にしてみましょう。 word_list = ["aardvark", "baboon", "camel"] #TODO """ import random word_list = ["aardvark", "baboon", "camel"] n=random.randint(0,len(word_list)) chosen_word=word_list[n-1] print("Pssst, the solution is {}.".format(chosen_word)) guess=input("guess a letter:") check_list=list(chosen_word) for i in range(len(chosen_word)): if guess in check_list: print("Right") pop_check=check_list.pop(0) else: print("Wrong") pop_check=check_list.pop(0) #ifの判断が正確にできていなくて、解決方法が不明。。 """ import random word_list = ["aardvark", "baboon", "camel"] chosen_word=word_list[random.randrange(len(word_list))] print("Pssst, the solution is {}.".format(chosen_word)) guess=input("guess a letter:") for char in chosen_word: if char == guess: print("Right") else: print("Wrong") ``` 答え: ![](https://i.imgur.com/qwpA9hJ.png) ::: ## Step - 2 * TODO-1: - Create an empty List called display. #For each letter in the chosen_word, add a "_" to 'display'. #So if the chosen_word was "baboon", display should be ["_", "_", "_", "_", "_", "_"] with 6 "_" representing each letter to guess. * TODO-2: - Loop through each position in the chosen_word; #If the letter at that position matches 'guess' then reveal that letter in the display at that position. #e.g. If the user guessed "b" and the chosen word was "baboon", then display should be ["b", "_", "b", "_", "_", "_"]. * TODO-3: - Print 'display' and you should see the guessed letter in the correct position and every other letter replace with "_". #Hint - Don't worry about getting the user to guess the next letter. We'll tackle that in step 3. :::warning * TODO-1: - ランダムに選ばれた単語と同じ長さで、「"_"」を要素として持つリストを作成してください。 * TODO-2: - ループでchosen_wordの文字を取り出し、もし推測した(guess)が取り出した文字と一致した場合、取り出した文字と同じIndexのList要素を、該当文字にしてください。 * TODO-3: - リストを表示させると、推測された文字が表示され、その他の要素はすべて「"_"」です ::: **出力例1:** ![](https://i.imgur.com/OdNaIat.png) :::info code: ```python= """ empty_list=[] for i in range(len(chosen_word)): empty_list.append("_") print(empty_list) guess=input("guess a letter:") for i in range(len(chosen_word)): if guess in chosen_word: #一部のlistの要素を文字に変換させる方法が分かりません。 """ for i in range(len(chosen_word)): if guess == chosen_word[i]: empty_list = ?? """ """ import random word_list = ["aardvark", "baboon", "camel"] chosen_word=random.choice(word_list) print("Pssst, the solution is {}.".format(chosen_word)) display=[] for i in range(len(chosen_word)): display.append("_") print(display) guess=input("guess a letter:") for i in range(len(chosen_word)): if chosen_word[i]==guess: display[i]=guess print(display) ``` 答え: ![](https://i.imgur.com/SEYoLl9.png) ::: ## Step - 3 * TODO-1: - Use a while loop to let the user guess again. The loop should only stop once the user has guessed all the letters in the chosen_word and 'display' has no more blanks ("_"). Then you can tell the user they've won. :::warning * TODO-1: ユーザーに再度推測させるためにwhileループを使用します。 * ユーザーが選んだ単語の文字をすべて当て、リストに("_")がなくなった時点でループを停止します。 * 最後に、"you win"を表示してください。 ::: **出力例1:** ![](https://i.imgur.com/QjdeNg3.png) :::info code: ```python= """ guess=input("guess a letter:") while guess in cjosen_word: #Step-2同様、list内の一部を変換させる方法が分かりません。 """ import random word_list = ["aardvark", "baboon", "camel"] chosen_word=random.choice(word_list) print("Pssst, the solution is {}.".format(chosen_word)) display=[] for i in range(len(chosen_word)): display.append("_") print(display) end_of_game=False while not end_of_game: guess = input("guess a letter:") for i in range(len(chosen_word)): if chosen_word[i] == guess: display[i] = guess print(display) if "_" not in display: end_of_game = True print("You win") ``` 答え: ![](https://i.imgur.com/20dmaRB.png) ::: ## Step - 4 * TODO-1: - Create a variable called 'lives' to keep track of the number of lives left. #Set 'lives' to equal 6. * TODO-2: - If guess is not a letter in the chosen_word, #Then reduce 'lives' by 1. #If lives goes down to 0 then the game should stop and it should print "You lose." * TODO-3: - print the ASCII art from 'stages' that corresponds to the current number of 'lives' the user has remaining. * TODO-4 - Update the word list to use the 'word_list' :::warning * TODO-1: ライフの数を記録するために「lives」という変数を作成します。 * 「lives」を「6」に設定します。 * TODO-2: 推測した単語(guess)がchosen_wordに含まれていない場合 * 「lives」を1減らします。 * もしlivesが0になったら、ゲームは停止し、"You lose "と表示されるはずです。 * TODO-3: ユーザーの現在の残りの「lives」の数に対応するASCIIアートをリスト「stages」から表示してください。 * TODO-4: 元のword_listの代わりに、以下の新しいword_listをご利用ください。 ::: [最終結果(実行例)](https://replit.com/@eclairsameal/Hangman#main.py) * テストしやすいように答えを表示しています。 ### ASCII art and word_list ```python= stages = [''' +---+ | | O | /|\ | / \ | | ========= ''', ''' +---+ | | O | /|\ | / | | ========= ''', ''' +---+ | | O | /|\ | | | ========= ''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | | | | | ========= ''', ''' +---+ | | O | | | | ========= ''', ''' +---+ | | | | | | ========= '''] word_list = [ 'abruptly', 'absurd', 'abyss', 'affix', 'askew', 'avenue', 'awkward', 'axiom', 'azure', 'bagpipes', 'bandwagon', 'banjo', 'bayou', 'beekeeper', 'bikini', 'blitz', 'blizzard', 'boggle', 'bookworm', 'boxcar', 'boxful', 'buckaroo', 'buffalo', 'buffoon', 'buxom', 'buzzard', 'buzzing', 'buzzwords', 'caliph', 'cobweb', 'cockiness', 'croquet', 'crypt', 'curacao', 'cycle', 'daiquiri', 'dirndl', 'disavow', 'dizzying', 'duplex', 'dwarves', 'embezzle', 'equip', 'espionage', 'euouae', 'exodus', 'faking', 'fishhook', 'fixable', 'fjord', 'flapjack', 'flopping', 'fluffiness', 'flyby', 'foxglove', 'frazzled', 'frizzled', 'fuchsia', 'funny', 'gabby', 'galaxy', 'galvanize', 'gazebo', 'giaour', 'gizmo', 'glowworm', 'glyph', 'gnarly', 'gnostic', 'gossip', 'grogginess', 'haiku', 'haphazard', 'hyphen', 'iatrogenic', 'icebox', 'injury', 'ivory', 'ivy', 'jackpot', 'jaundice', 'jawbreaker', 'jaywalk', 'jazziest', 'jazzy', 'jelly', 'jigsaw', 'jinx', 'jiujitsu', 'jockey', 'jogging', 'joking', 'jovial', 'joyful', 'juicy', 'jukebox', 'jumbo', 'kayak', 'kazoo', 'keyhole', 'khaki', 'kilobyte', 'kiosk', 'kitsch', 'kiwifruit', 'klutz', 'knapsack', 'larynx', 'lengths', 'lucky', 'luxury', 'lymph', 'marquis', 'matrix', 'megahertz', 'microwave', 'mnemonic', 'mystify', 'naphtha', 'nightclub', 'nowadays', 'numbskull', 'nymph', 'onyx', 'ovary', 'oxidize', 'oxygen', 'pajama', 'peekaboo', 'phlegm', 'pixel', 'pizazz', 'pneumonia', 'polka', 'pshaw', 'psyche', 'puppy', 'puzzling', 'quartz', 'queue', 'quips', 'quixotic', 'quiz', 'quizzes', 'quorum', 'razzmatazz', 'rhubarb', 'rhythm', 'rickshaw', 'schnapps', 'scratch', 'shiv', 'snazzy', 'sphinx', 'spritz', 'squawk', 'staff', 'strength', 'strengths', 'stretch', 'stronghold', 'stymied', 'subway', 'swivel', 'syndrome', 'thriftless', 'thumbscrew', 'topaz', 'transcript', 'transgress', 'transplant', 'triphthong', 'twelfth', 'twelfths', 'unknown', 'unworthy', 'unzip', 'uptown', 'vaporize', 'vixen', 'vodka', 'voodoo', 'vortex', 'voyeurism', 'walkway', 'waltz', 'wave', 'wavy', 'waxy', 'wellspring', 'wheezy', 'whiskey', 'whizzing', 'whomever', 'wimpy', 'witchcraft', 'wizard', 'woozy', 'wristwatch', 'wyvern', 'xylophone', 'yachtsman', 'yippee', 'yoked', 'youthful', 'yummy', 'zephyr', 'zigzag', 'zigzagging', 'zilch', 'zipper', 'zodiac', 'zombie', ] ``` :::info code: ```python= """ lives = 6 if guess in chosen_word: lives-=1 elif lives==0: print("You lose") stages = [''' +---+ | | O | /|\ | / \ | | ========= ''', ''' +---+ | | O | /|\ | / | | ========= ''', ''' +---+ | | O | /|\ | | | ========= ''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | | | | | ========= ''', ''' +---+ | | O | | | | ========= ''', ''' +---+ | | | | | | ========= '''] #TODO-3からの方法がよくわかりません。。 """ import random stages = [''' +---+ | | O | /|\ | / \ | | ========= ''', ''' +---+ | | O | /|\ | / | | ========= ''', ''' +---+ | | O | /|\ | | | ========= ''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | | | | | ========= ''', ''' +---+ | | O | | | | ========= ''', ''' +---+ | | | | | | ========= '''] word_list = [ 'abruptly', 'absurd', 'abyss', 'affix', 'askew', 'avenue', 'awkward', 'axiom', 'azure', 'bagpipes', 'bandwagon', 'banjo', 'bayou', 'beekeeper', 'bikini', 'blitz', 'blizzard', 'boggle', 'bookworm', 'boxcar', 'boxful', 'buckaroo', 'buffalo', 'buffoon', 'buxom', 'buzzard', 'buzzing', 'buzzwords', 'caliph', 'cobweb', 'cockiness', 'croquet', 'crypt', 'curacao', 'cycle', 'daiquiri', 'dirndl', 'disavow', 'dizzying', 'duplex', 'dwarves', 'embezzle', 'equip', 'espionage', 'euouae', 'exodus', 'faking', 'fishhook', 'fixable', 'fjord', 'flapjack', 'flopping', 'fluffiness', 'flyby', 'foxglove', 'frazzled', 'frizzled', 'fuchsia', 'funny', 'gabby', 'galaxy', 'galvanize', 'gazebo', 'giaour', 'gizmo', 'glowworm', 'glyph', 'gnarly', 'gnostic', 'gossip', 'grogginess', 'haiku', 'haphazard', 'hyphen', 'iatrogenic', 'icebox', 'injury', 'ivory', 'ivy', 'jackpot', 'jaundice', 'jawbreaker', 'jaywalk', 'jazziest', 'jazzy', 'jelly', 'jigsaw', 'jinx', 'jiujitsu', 'jockey', 'jogging', 'joking', 'jovial', 'joyful', 'juicy', 'jukebox', 'jumbo', 'kayak', 'kazoo', 'keyhole', 'khaki', 'kilobyte', 'kiosk', 'kitsch', 'kiwifruit', 'klutz', 'knapsack', 'larynx', 'lengths', 'lucky', 'luxury', 'lymph', 'marquis', 'matrix', 'megahertz', 'microwave', 'mnemonic', 'mystify', 'naphtha', 'nightclub', 'nowadays', 'numbskull', 'nymph', 'onyx', 'ovary', 'oxidize', 'oxygen', 'pajama', 'peekaboo', 'phlegm', 'pixel', 'pizazz', 'pneumonia', 'polka', 'pshaw', 'psyche', 'puppy', 'puzzling', 'quartz', 'queue', 'quips', 'quixotic', 'quiz', 'quizzes', 'quorum', 'razzmatazz', 'rhubarb', 'rhythm', 'rickshaw', 'schnapps', 'scratch', 'shiv', 'snazzy', 'sphinx', 'spritz', 'squawk', 'staff', 'strength', 'strengths', 'stretch', 'stronghold', 'stymied', 'subway', 'swivel', 'syndrome', 'thriftless', 'thumbscrew', 'topaz', 'transcript', 'transgress', 'transplant', 'triphthong', 'twelfth', 'twelfths', 'unknown', 'unworthy', 'unzip', 'uptown', 'vaporize', 'vixen', 'vodka', 'voodoo', 'vortex', 'voyeurism', 'walkway', 'waltz', 'wave', 'wavy', 'waxy', 'wellspring', 'wheezy', 'whiskey', 'whizzing', 'whomever', 'wimpy', 'witchcraft', 'wizard', 'woozy', 'wristwatch', 'wyvern', 'xylophone', 'yachtsman', 'yippee', 'yoked', 'youthful', 'yummy', 'zephyr', 'zigzag', 'zigzagging', 'zilch', 'zipper', 'zodiac', 'zombie', ] lives = 6 chosen_word=random.choice(word_list) print("Pssst, the solution is {}.".format(chosen_word)) display = [] for i in range(len(chosen_word)): display.append("_") print(display) end_of_game = False while not end_of_game: guess = input("guess a letter:") if not guess in chosen_word: lives -= 1 else: for i in range(len(chosen_word)): if chosen_word[i] == guess: display[i] = guess if lives == 0: end_of_game = True print("You lose") print(stages[lives]) print(display) if "_" not in display: end_of_game = True print("You win") ``` 答え: ![](https://i.imgur.com/BgPRT5p.png) ![](https://i.imgur.com/dvHcX5C.png) ![](https://i.imgur.com/wPcIK1e.png) :::