Python Hangman Game Program Error

Started by
6 comments, last by gaurav99 1 year ago

Hello, I am Gaurav.

I am new in this community and don't know if it is the right place to talk about it. Well, I am writing a program to make a hangman game in Python but it shows some error at the time of execution I don't know what I am missing in coding.

Source Code:

import random

import time

import os

def play_again():

question = 'Do You want to play again? y = yes, n = no \n'

play_game = input(question)

while play_game.lower() not in ['y', 'n']:

play_game = input(question)

if play_game.lower() == 'y':

return True

else:

return False

def hangman(word):

display = '_' * len(word)

count = 0

limit = 5

letters = list(word)

guessed = []

while count < limit:

guess = input(f'Hangman Word: {display} Enter your guess: \n').strip()

while len(guess) == 0 or len(guess) > 1:

print('Invalid input. Enter a single letter\n')

guess = input(

f'Hangman Word: {display} Enter your guess: \n').strip()

if guess in guessed:

print('Oops! You already tried that guess, try again!\n')

continue

if guess in letters:

letters.remove(guess)

index = word.find(guess)

display = display[:index] + guess + display[index + 1:]

else:

guessed.append(guess)

count += 1

if count == 1:

time.sleep(1)

print(' _____ \n'

' | \n'

' | \n'

' | \n'

' | \n'

' | \n'

' | \n'

'__|__\n')

print(f'Wrong guess: {limit - count} guesses remaining\n')

elif count == 2:

time.sleep(1)

print(' _____ \n'

' | | \n'

' | | \n'

' | \n'

' | \n'

' | \n'

' | \n'

'__|__\n')

print(f'Wrong guess: {limit - count} guesses remaining\n')

elif count == 3:

time.sleep(1)

print(' _____ \n'

' | | \n'

' | | \n'

' | | \n'

' | \n'

' | \n'

' | \n'

'__|__\n')

print(f'Wrong guess: {limit - count} guesses remaining\n')

elif count == 4:

time.sleep(1)

print(' _____ \n'

' | | \n'

' | | \n'

' | | \n'

' | O \n'

' | \n'

' | \n'

'__|__\n')

print(f'Wrong guess: {limit - count} guesses remaining\n')

elif count == 5:

time.sleep(1)

print(' _____ \n'

' | | \n'

' | | \n'

' | | \n'

' | O \n'

' | /|\ \n'

' | / \ \n'

'__|__\n')

print('Wrong guess. You\'ve been hanged!!!\n')

print(f'The word was: {word}')

if display == word:

print(f'Congrats! You have guessed the word \'{word}\' correctly!')

break()

def play_hangman():

print('\nWelcome to Hangman\n')

name = input('Enter your name: ')

print(f'Hello {name}! Best of Luck!')

time.sleep(1)

print('The game is about to start!\nLet\'s play Hangman!')

time.sleep(1)

os.system('cls' if os.name == 'nt' else 'clear')

words_to_guess = [

'january', 'border', 'image', 'film', 'promise', 'kids',

'lungs', 'doll', 'rhyme', 'damage', 'plants', 'hello', 'world'

]

play = True:

while play:

word = random.choice(words_to_guess)

hangman(word)

play = play_again()

print('Thanks For Playing! We expect you back again!')

exit()

if __name__ == '__main__':

play_hangman()

Well, I also checked and took a reference from here but I don't know what I am missing in coding, can anyone give their suggestions on this?

Thanks

None

Advertisement

Hello and welcome to the forum.

Unfortunately your posted code is not usable, in Python whitespace is significant but the above text doesn't have any indentation anymore.

So please post your code again, except please use a code-box for this. In the bar above the area where you type is a symbol that looks like a page of text with a “<>” on it (next to the quotes symbol). Click that page symbol and you'll get a code box for publishing your source code.

Secondly, it would be useful to also post output and the error trace. I don't know what you did to get the error so for me it's like finding a needle in a haystack in this way. These error messages have line numbers associated with them, and for the other readers it's helpful if you indicate what line is indicated with a message, like

         transform="matrix(0.99114293,0,0,0.81389675,44.655895,38.507142)"
         style="display:inline">
         ...
      </g>
    </g>
  </g>
</svg>"""

import xml.etree.ElementTree as ET

root = ET.fromstring(data)
print("Root:")
print("=====")  oops  # <--- line 31
print(root)

The error trace:

  File "/newhome/alberth/x.py", line 31
    print("=====")  oops
                    ^^^^
SyntaxError: invalid syntax

My code is not complete, but this is an example, note the “line 31” in the error message, I added “# <--- line 31” to the code to let readers know what line that is.

Well, thanks a lot for your kind response. Sure I will try it.

None

gaurav99 said:
I am new in this community and don't know if it is the right place to talk about it. Well, I am writing a program

Well, the Writing forum is for discussion of story writing for games, not writing code. Well, your thread has been moved to the For Beginners forum. Well, welcome to the forums.

-- Tom Sloper -- sloperama.com

@Tom Sloper Thanks a lot for your kind response and kind guidance.

Thanks again.

None

Ah, I didn't realize you linked to a page with the original source code.

I copy/paste-ed your code and the example code in 2 different files and let my editor show the differences. That resulted in (I just took a part with “real” changes):

left your code from the forum; right the example code from you link

As you can see all lines of your file are against the left margin as the forum removed the leading whitespace. At the right there is highlighted leading space as the editor is telling me “these are missing”. I assume you have leading space in your copy of the code, so that should all be fine.

The second thing to note is that if you ignore changes in whitespace, the text is the same throughout the file, except at 2 lines that are also in the screenshot.

In your code it says “break()” while the example code says “break” around line 105. Also, you have an additional colon after “play = True” around line 121.

Both the “()” and the “:” are incorrect. Python however attaches special meaning to such additions, and it tries to follow that meaning and then gets confused and throws an error.

Yes, it works.

Thanks a lot for your kind response and suggestions.

Thanks again.

None

This topic is closed to new replies.

Advertisement