Create a word hangman game.
The word hangman is a simple game. You will provide a word with the first letter. Rest of the letters will be blank. For example,
s_ _ _ _ _ _ _ _ _
The player has to guess the letters in the word. And the player will guess one letter at a time. If the letter exists in the word, it will appear to the player.
If the letter doesn’t exist in the word, it will be counted as a wrong try.
If the player exceeds the number of the wrong tries, the player will lose.
Otherwise, the player will win.
importrandomdefselected_a_word(): words= ['working', 'hard', 'makes', 'things', 'easier', 'congrats', 'programming', 'hero'] word=words[random.randint(0, len(words)-1)] returnworddefget_blank_word(word): blank_word=word[0] foriinrange(1, len(word)): blank_word+='_'returnblank_worddefword_hangman(word, so_far, letter, try_left): bad_try=Trueforiinrange(0, len(word)): ifword[i] ==letter: so_far=so_far[:i]+letter+so_far[i+1:] bad_try=Falseifbad_tryisTrue: try_left-=1print('Wrong Try Left: ', try_left) print('so far you got: ', so_far) ifword==so_far: print('YAY!!! You Win') eliftry_left==0: print('Opps!!! You Lost') else: next_letter=input ('Enter your next letter: ') word_hangman(word, so_far, next_letter, try_left) # play the gameword=selected_a_word() clue_word=get_blank_word(word) word_hangman(word, clue_word, word[0], 5)
Take a deep breather. Start reading the code.
Reading other’s code and trying to understand will be a common activity for a programmer.
So, practice it here.
If you don't understand any line, feel free to ask us a question.
tags: programming-hero
python
python3
problem-solving
programming
coding-challenge
interview
learn-python
python-tutorial
programming-exercises
programming-challenges
programming-fundamentals
programming-contest
python-coding-challenges
python-problem-solving