• Our team is looking to connect with folks who use email services provided by Plesk, or a premium service. If you'd like to be part of the discovery process and share your experiences, we invite you to complete this short screening survey. If your responses match the persona we are looking for, you'll receive a link to schedule a call at your convenience. We look forward to hearing from you!
  • We are looking for U.S.-based freelancer or agency working with SEO or WordPress for a quick 30-min interviews to gather feedback on XOVI, a successful German SEO tool we’re looking to launch in the U.S.
    If you qualify and participate, you’ll receive a $30 Amazon gift card as a thank-you. Please apply here. Thanks for helping shape a better SEO product for agencies!
  • The BIND DNS server has already been deprecated and removed from Plesk for Windows.
    If a Plesk for Windows server is still using BIND, the upgrade to Plesk Obsidian 18.0.70 will be unavailable until the administrator switches the DNS server to Microsoft DNS. We strongly recommend transitioning to Microsoft DNS within the next 6 weeks, before the Plesk 18.0.70 release.
  • The Horde component is removed from Plesk Installer. We recommend switching to another webmail software supported in Plesk.

Question Python Hangman Game Program Error

gulshan212

New Pleskian
Hello this is Gulshan Negi
Well, I am writing a program for making the Hangman game, but it shows some error at the time of execution, I don't know what I am missing here.

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 also checked and took a reference from here, but I don't know what is wrong with the coding.
Can anyone give their suggestion on this.
Thanks
 
Hello this is Gulshan Negi
Well, I am writing a program for making the Hangman game, but it shows some error at the time of execution, I don't know what I am missing here.

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: https://casinosanalyzer.com/online-casinos/kansas-usa has reviews
about online gambling in Kansas State.
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 also checked and took a reference from here, but I don't know what is wrong with the coding.
Can anyone give their suggestion on this.
Thanks
Hi gulshan! I have thought on your issue and, I hope my help will be still useful, detected that this code would solve the problem:
import random

def select_word():
words = ['magazine', 'stars', 'computer', 'python', 'organization']
return random.choice(words)

def is_guessed(word, guessed_letter_list):
return all(letter in guessed_letter_list for letter in word)

def guessed_word(word, guessed_letter_list):
return ''.join([letter if letter in guessed_letter_list else '_' for letter in word])

def available_letters(guessed_letter_list):
return ''.join(letter for letter in 'abcdefghijklmnopqrstuvwxyz' if letter not in guessed_letter_list)

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)
guessed_letter_list = set()

while chances != 0:
if not is_guessed(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: ").lower()[0]
print('\n' * 50)

if guess not in guessed_letter_list:
guessed_letter_list.add(guess)
print("NICE YOU GUSESSED THE RIGHT LETTER! ", guessed_word(word, guessed_letter_list))

elif guess in guessed_letter_list:
print("SORRY! YOU HAVE GUSSED THIS LETTER ALREADY! ", guessed_word(word, guessed_letter_list))

else:
print(" SORRY! THE LETTER IS NOT IN WORD", guessed_word(word, guessed_letter_list))
chances -= 1

else:
print("YOU WON!")
break

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

word = select_word()
hangman_game(word)

Let me think if it helps,
Cheers, Teodoro
 
  1. Fix the code indentation.
  2. Correct the error in the line while (chances != 0):.
  3. Fix the is_guessed function:
pythonCopy code
def is_guessed(word, guessed_letter_list):
return all(letter in guessed_letter_list for letter in word)

  1. Correct the conditional statement:
pythonCopy code
if guessInLowerCase in guessed_letter_list:

  1. Move the win condition inside the while loop.
  2. After the game ends, use break to exit the loop and return to exit the function.
 
You should fix the part of your code that checks if the word is guessed. Make sure it properly checks if all the letters in the word have been guessed. Also, add a colon at the end of the conditional statement where you check if the guessed letter is already in the list.
 
Back
Top