Python Hangman Game Program Error

Hello this is Gulshan Negi
Well, I am writing a program for making Hangman game using Python, but it shows some errors at the time of execution, I don’t know what wrong I am doing.
Here is my source code:

import random
def select_word():
words_in_computer_memory = [‘magazine’,‘stars’,‘computer’,‘python’,‘organisation’]
word = random.choice(words_in_computer_memory)
return word
def is_gussed(word, guessed_letter_list):
count=0
for letters in word:
if letters in guessed_letter_list:
count+=1
if count==len(word):
return True
else:
return False
def guessed_word(word, guessed_letter_list):
string=“”
for key in word:
if key in guessed_letter_list:
string+=key
else:
string+="_ "
return string
def available_letters(guessed_letter_list):

string=""
count=0
s='abcdefghijklmnopqrstuvwxyz'
for letter in s:
    if letter in guessed_letter_list:
        count+=1
    else:
        string+=letter
return string

def hangman_game(word):
length=len(word)
print(‘’‘------------------WELCOME TO HANGMAN GAME---------------------------
O
/|\
/ \
‘’’)
print("The word you have to guess is of ",length, “letters long.”)
chances=2*len(word)
i=0
guessed_letter_list=[]
while (chances!=0):

    if word!=guessed_word(word, guessed_letter_list):
        print("You Got", chances, "Chances.")
        print("Letters you can enter should be from these ",available_letters(guessed_letter_list))
        guess=input("ENTER A LETTER ")
        print('\n'*50)

        guessInLowerCase = guess[0].lower()      
        if guessInLowerCase  in guessed_letter_list:
            print("SORRY! YOU HAVE GUSSED THIS LETTER ALREADY! ",guessed_word(word, guessed_letter_list))
        elif guessInLowerCase not in word: 
            print(" SORRY! THE LETTER IS NOT IN WORD",guessed_word(word, guessed_letter_list))
            chances-=1
        else:
            guessed_letter_list.append(guessInLowerCase)
            print("NICE YOU GUSESSED THE RIGHT LETTER! ",guessed_word(word, guessed_letter_list))
       
    elif word==guessed_word(word, guessed_letter_list):
        print("YOU WON!")
        break

else:
    print('''
    ********************************************

YOU LOSS!!
O
/|\
/ \
******************************************‘’')
print(‘The word was’,word,)

word = select_word()
hangman_game(word)

I don’t know what I am missing in coding. Can anyone give their suggestions on this?
Thanks

It seems like you have written a code for a Hangman game using Python. The code is missing some indentation and there are some errors in the syntax, but I can understand the general flow of the game.

Here is a breakdown of what the code is doing:

  1. The program starts by initializing a string variable called string to an empty string and an integer variable called count to 0. It also creates a string variable s containing all the letters of the English alphabet.
  2. The guessed_word function takes a word and a list of guessed letters and returns a string representing the letters of the word that have been correctly guessed so far.
  3. The available_letters function takes a list of guessed letters and returns a string representing the letters of the alphabet that have not been guessed yet.
  4. The hangman_game function takes a word as input and starts the game by printing a welcome message and the initial state of the hangman. It then initializes the number of chances the player has based on the length of the word, and a list of guessed letters.
  5. A while loop is used to keep the game running until the player wins or loses. The loop checks if the guessed word matches the actual word, and if it does, the player wins and the loop breaks. Otherwise, the loop prompts the player for a guess and checks if the guess has already been made or if it is not in the word. If the guess is valid, it adds it to the list of guessed letters and updates the guessed word.
  6. If the loop runs out of chances, the player loses and the actual word is displayed.

The code could be improved by adding more error handling and making the code more modular, but overall it looks like a good start for a Hangman game.

Thanks a lot for your kind response and time.
Thanks again

1 Like