Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Already Used | Main Body
Beginner Final Project: Hangman

book
Already Used

What if the user is trying to use the letter that has been already used? We need to 'punish' the user by decreasing warnings_remaining(or guesses_remaining if there are no warnings left). We are saving all letters that have been used into the used_letters variable.

**Scroll down to see the full code!

Oppgave

Swipe to start coding

  1. Decrease the warnings_remaining.
  2. Set condition if the warnings_remaining is lower than 0.
  3. Decrease the guesses_remaining.

Løsning

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():
pass
elif guess in set(used_letters):
# Decrease the warnings_remaining
warnings_remaining -= 1
print('Oops! You`ve already guessed that letter')
# Set condition if 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(gameword):
pass
else:
pass
if guesses_remaining <= 0:
print('Sorry, you ran out of guesses. The word was: ', gameword)
break
else:
print('Congratulations, you won!' + 'The gameword is: ', gameword)

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3
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():
pass
elif guess in set(used_letters):
# Decrease the warnings_remaining
___
print('Oops! You`ve already guessed that letter.')
# Condition: the warnings_remaining is lower than 0
___
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(gameword):
pass
else:
pass
if guesses_remaining <= 0:
print('Sorry, you ran out of guesses. The word was: ', gameword)
break
else:
print('Congratulations, you won!' + 'The gameword is: ', gameword)

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

We use cookies to make your experience better!
some-alt