Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Is not a Letter | Main Body
Beginner Final Project: Hangman

book
Is not a Letter

What if the user input & or @? We need to 'punish' the user by decreasing number of warnings (or guesses if there are no warnings left). Only one symbol ! means that the user is asking for the hint. We need to take into account these cases.

Task

Swipe to start coding

  1. Set condition if the input is '!'.
  2. Print possible matches using the show_possible_matches function and the get_guessed_word(gameword, used_letters) as an argument.
  3. Decrease the warnings_remaining by one.
  4. Set condition if there are less than 0 (not included) warnings.
  5. Decrease the guesses_reamaining by one.

Solution

def hangman(gameword):
used_letters = []
guesses_remaining, warnings_remaining = 6, 3
print('A gameword is', len(gameword), 'letters long')
while True:
if not is_word_guessed(gameword, used_letters):
print('You have', int(guesses_remaining), 'guess(es) left and', int(warnings_remaining), 'warning(s) left!' + 'Available letters: ',get_available_letters(used_letters) + 'Game word: ', get_guessed_word(gameword, used_letters))
guess = str.lower(input('Please, input a letter: '))
if not guess.isalpha():
# Condition: the guess is '!'
if guess == '!':
# Print possible matches
print('Possible word matches are: ',
show_possible_matches(get_guessed_word(gameword, used_letters)))
else:
print('That is not a valid symbol.')
# Decrease the warnings_remaining
warnings_remaining -= 1
# Condition: the warnings_remaining is lower than 0
if warnings_remaining < 0:
print('You have no warnings left so you lose 1 guess!')
# Decrease the guesses_remaining
guesses_remaining -= 1
else:
print('You loose 1 warning!')
elif guess in set(used_letters):
pass
elif guess in set(gameword):
pass
else:
pass

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
def hangman(gameword):
used_letters = []
guesses_remaining, warnings_remaining = 6, 3
print('A gameword is', len(gameword), 'letters long')
while True:
if not is_word_guessed(gameword, used_letters):
print('You have', int(guesses_remaining), 'guess(es) left and', int(warnings_remaining), 'warning(s) left!' + 'Available letters: ',get_available_letters(used_letters) + 'Game word: ', get_guessed_word(gameword, used_letters))
guess = str.lower(input('Please, input a letter: '))
if not guess.isalpha():
# Condition: the guess is '!'
if guess == ___:
# Print possible matches
print('Possible word matches are: ', ___(get_guessed_word(___)))
else:
print('That is not a valid symbol.')
# Decrease the warnings_remaining
___ -= ___
# Condition: the warnings_remaining is lower than 0
if warnings_remaining ___:
print('You have no warnings left so you lose 1 guess!')
# Decrease the guesses_remaining
___
else:
print('You loose 1 warning!')
elif guess in set(used_letters):
pass
elif guess in set(gameword):
pass
else:
pass

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt