Check if letters from word are all in string Hangman


Keywords:python 


Question: 

I'm trying to create a simple hangman game in Python. I've come quite far for a beginner I guess :) The only thing I can't accomplish is checking wether the word is actually guessed. I need to check if all the letters are actually in a list I've created, yet I'm not sure how. I do realise there was a topic regarding this, this didn't help me tho.

import random

with open('randomlist.txt') as bestand:
woordenlijst = bestand.read().splitlines()

print("Welcome to Hangman.")
naam = input("What is your name? ")
print("Hello", naam, ", the computer selecter a secret word.")

woord = random.choice(woordenlijst)
lengte_woord = len(woord)
ingevoerde_letters = []

galg = []

i = 0

print("The length of the word is: \n")
while i < lengte_woord:
    print("_", end=" ")
    i = i + 1

levens = 0
invoer = " "

print(woord)

while levens <= 10:
    letter = input("Letter: ")
    if letter in ingevoerde_letters:
        print("You already tried this letter.")
    elif letter in woord:
        print("This letter is in the word. We'll fill it in for you.")
        i = 0
        while i < lengte_woord:           
            if woord[i] == letter:
                print(letter, end=" ")
                ingevoerde_letters.append(letter)
                i = i + 1
            elif woord[i] in ingevoerde_letters:
                print(woord[i], end=" ")
                i = i + 1
            else:
                print("_", end=" ")
                i = i + 1                            
    else:
        ingevoerde_letters.append(letter)
        print("This letter is not in the word", galg[levens])
        levens = levens + 1       
print(galg[10], "\nGame over!")                 

2 Answers: 

There are some problems in your code. For example, when guessing a wrong letter, this letter is never added to the galg list you later use to print wrong guess. So either replace galg with ingevord_letter or add the letter to both ingevord_letter and galg. Like this:

else:
    ingevoerde_letters.append(letter)
    galg.append(letter)

A second issue is in your read file. When you say .splitline(), you make woord contain whole lines (or everything encompassed between \n). I think you want .split() instead to choose 1 random word from the text.

For your actual question, one way to finish the game upon successful guesses, is to check the letter collection against the collection of letters in the word. See the isWon function. ref this answer

import random
def isWon(mysteryWord, lettersGuessed):
    return set(lettersGuessed) == set(mysteryWord)

with open('randomlist.txt') as bestand:
    woordenlijst = bestand.read().splitlines()

print("Welkom bij Galgje, voor we beginnen willen we graag het volgende aan je vragen.")
naam = input("Wat is je naam? ")
print("Hallo", naam, ", leuk dat je wilt spelen. De computer heeft een willekeurig woord voor je geselecteerd.")

woord = random.choice(woordenlijst)
lengte_woord = len(woord)
ingevoerde_letters = []

galg = []

i = 0

print("De lengte van het woord is: \n")
while i < lengte_woord:
    print("_", end=" ")
    i = i + 1

levens = 0
invoer = " "

print(woord)

while levens <= 10:
    letter = input("Geef een letter: ")
    if letter in ingevoerde_letters:
        print("Deze letter heb je al ingevoerd.")
    elif letter in woord:
        print("Deze letter zit in het woord. We vullen het voor je in.")
        i = 0
        while i < lengte_woord:           
            if woord[i] == letter:
                print(letter, end=" ")
                ingevoerde_letters.append(letter)
                i = i + 1
            elif woord[i] in ingevoerde_letters:
                print(woord[i], end=" ")
                i = i + 1
            else:
                print("_", end=" ")
                i = i + 1           
    else:
        ingevoerde_letters.append(letter)
        galg.append(letter)
        print("Deze letter zit niet in het woord", galg[levens])
        levens = levens + 1  
    if (isWon(ingevoerde_letters, woord)):
      #successfull code here
      break
print(galg, "\nGame over!")  


You have most of what you need to determine this, and while there are far more efficient ways to do what you've done (check out sets in Python), the following will work just fine:

# Somewhere towards the top of the script
total_letters_guessed = 0

...

while levens <= 10:
    ...
    if woord[i] == letter:
        print(letter, end=" ")
        ingevoerde_letters.append(letter)
        i = i + 1
        total_letters_guessed += 1

    ...

    # Before your loop ends
    if total_letters_guessed == lengte_woord:
        print("You won!")