0

Im trying to create a hangman game and I've watched a video about how someone created a similar hangman game and I'm trying to learn from them. However im changing some code here and there to learn on my own. This is what I have so far:

import random
word_list = ['Duck', 'Dog', 'Cat']

def correct_word():
    word = random.choice(word_list)
    return word.upper()

def play(word):
    word_complete = "_ " * len(word)
    guess_letter = []
    guess_word = []
    num_of_guesses = 0
    num_of_guesses_tot = 0
    guessed = False

    print('HANGMAN BY NILS')
    print(word_complete)

    while not guessed and num_of_guesses <= 6:
        guess = input('Gissa på ett ord eller en bokstav!').upper()

        if len(guess) == 1 and guess.isalpha():
            if guess in guess_letter:
                print('Already guessed', guess)

            elif guess not in word:
                print(guess, "doesn't exist in the word")
                num_of_guesses += 1
                num_of_guesses_tot += 1
                guess_letter.append(guess)
            
            else:
                print('Correct', guess, 'is in the word')
                guess_letter.append(guess)
                num_of_guesses_tot += 1

                word_store_list = word_complete.split()
                word_storage = word.split()
                index = word_storage.index(guess)

                word_store_list[index] = guess
                word_complete = ' '.join(map(str, word_store_list))

                if "_" not in word_complete:
                    guessed = True

                return word_complete

        elif len(guess) == len(word) and guess.isalpha():
            if guess in guess_word:
                print('Correct you guessed the word', word)
                num_of_guesses_tot += 1
            
            elif guess != word:
                print(guess, 'is not in the word')
                num_of_guesses += 1
                num_of_guesses_tot += 1
                guess_word.append(guess)
            
            else:
                guessed = True
                word_complete = word
        
        else:
            print('Inte en giltlig gissning!')
        
        print(word_complete)
        print("\n")
        
    if guessed:
        print('Grattis du vinner! ordet var', word)
        print('Dina totala felaktiga gissningar var:', num_of_guesses)
        print('Dina totala gissningar var:', num_of_guesses_tot)

def main():
    word = correct_word()
    play(word)

    while str(input("Kör om (Y/N)")).upper() == "Y":
        word = correct_word()
        play(word)

if __name__ == "__main__":
    main()

The problem I have with the code is that as soon as I enter a correct letter (not a word), the program crashes or stops. I dont know why it does this but I think it has to do with this else statment:

        else:
            print('Correct', guess, 'is in the word')
            guess_letter.append(guess)
            num_of_guesses_tot += 1

            word_store_list = word_complete.split()
            word_storage = word.split()
            index = word_storage.index(guess)

            word_store_list[index] = guess
            word_complete = ' '.join(map(str, word_store_list))

            if "_" not in word_complete:
                guessed = True

            return word_complete

Note: It only crashes when I enter a correct letter, so correct words, incorect letters and words still work. This is how he created the else statment in the tutorial (however I didnt underrstand what he was doing so I wanted to try creating the code on my own:

        else:
            print("Good job,", guess, "is in the word!")
            guess_letter.append(guess)
            word_as_list = list(word_complete)
            indices = [i for i, letter in enumerate(word) if letter == guess]
            for index in indices:
                word_as_list[index] = guess
            word_complete = "".join(word_as_list)
            if "_" not in word_complete:
                guessed = True

Any help appriciated! :)

3
  • Your Words module is missing Commented Jun 5, 2021 at 12:52
  • cannot reproduce the problem Commented Jun 5, 2021 at 12:52
  • @ZainUlAbidin Updated the code my fault Commented Jun 5, 2021 at 14:08

1 Answer 1

1

Your variable word is a string that does not contain white-space. Therefore, when you call word.split() you obtain a list with only a single element.

Instead, you can call index directly on the string:

...
word_store_list = word_complete.split()
# word_storage = word.split()               Removed this line
index = word.index(guess)
...

Following this, you find another problem: the game always resets itself. You need to remove the return statement at the end of that same else block.

As a suggestion, I believe that for your purpose the map() is unnecessary since the elements must already be strings. You can simply do the following:

word_complete = ' '.join(word_store_list) 

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.