Forum >> Programmazione Python >> Videogames >> Python Hangman Game Program Error

Pagina: 1

Hello this is Gulshan Negi
Well, I am writing a program for making Hangman Game in Python but it shows some error at the time of its execution.

Here is my 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'
' | OR \n'
' | \n'
' | \n'
'__| __\n' )
print ( f'Wrong guess: {limit - count} guesses remaining\n' )

elif count == 5 :
time.sleep( 1 )
print ( ' _____ \n'
' | | \n'
' | | \n'
' | | \n'
' | OR \n'
' | /|\ \n'
' | / \ \ n'
'__|__\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' ,
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 here. Can anyone give their suggestions on this?

Thanks



Hi Gulshan, I don't speak English, the following text is produced by google translator, sorry for any poor language skills




If you want help, learn how to format the code using the appropriate button ("<>") in the post editor, how you expose it is difficult to understand.
It would also be appropriate to report the traceback received ... things already indicated in previous post.




Coming to your code, it has several critical issues:
the most serious is the "count += 1: " code present in the "hangman" function, the ":" character generates a syntax error
Another criticality is given by the "while play:" line in the "play_hangman" function, since the "play" variable is not initialized, therefore it cannot be used.

The code itself is very questionable, in particular with regard to instructions such as "if count == 1 :" and related print, as well as the mode of intercepting user input which, however, stops at the first occurrence of the entered character.

I am attaching below a rehash to make your code functional at a minimum, I haven't studied it well and it is limited to what I understand it is needed, but it should work anyway; see and understand the differences in logic

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

    return False


def hangman(word):
    display = '_' * len(word)
    count = 0
    limit = 5
    letters = list(word)
    guessed = []
    print(f'You have {limit} possible errors...\n')
    while count < limit:
        guess = input(f'Hangman Word: {display} Enter your guess:\n').strip()
        if len(guess) != 1 :
            print ( 'Invalid input. Enter a single letter \n' )
            continue
        if guess in guessed:
            print ( 'Oops! You already tried that guess, try again!\n' )
            continue

        if guess in letters:
            letters.remove(guess)
            for i in range(len(word)):
                if word[ i ] == guess:
                    display = display[:i] + guess + display[i+1:]
            print (' ' + '*' * (limit - count),
                   f'\n Good guess: {limit - count} guesses remaining\n')
        else :
            guessed.append(guess)
            count += 1
            print (' ' + '*' * (limit - count),
                   f'\n Wrong guess: {limit - count} guesses remaining\n')
        time.sleep(1)
        if display == word:
            print ( f'Congrats! You have guessed the word \' {word} \' correctly!' )
            break
        if count == limit:
            print ('\nThe word was: {word} ' )
            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']
    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() 
I hope it can help you

edit : I realized, now, that the post editor has altered the code, I corrected it


--- Ultima modifica di nuzzopippo in data 2023-05-05 14:50:21 ---
Fatti non foste a viver come bruti...
Thanks a lot for your kind response, I will try it.
Thanks again.
I noticed, now, that the post editor has altered the code, the correction of the code seems not to be allowed,
however in the statement "if word == guess:" the "word" is followed by an "i" in square brackets
Fatti non foste a viver come bruti...


Pagina: 1



Esegui il login per scrivere una risposta.