Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python hangman help
#1
Hello.

I need some help with my code. It has to be in the format I am posting. So when I run the game, it is basically supposed to keep asking for a guess until the number of guess reach 0 or until the word is found. Number of guesses start at 4 and decrease every single time the player inputs a wrong guess. The program is supposed to recognize repeated guesses of the same letter as a wrong guess. If the player guesses a letter correctly, the corresponding _ in the blanks are replaced with the letter, even if there are multiple occurrences of that letter within the word. If the player guesses all letters correctly and number of guesses is greater than 0, then the player wins. I have posted my code below. I think the problem is in the update_puzzle_string() block as it is updating the "temp" variable to give me the blanks with the word filled in. Please review it and let me know what I am doing wrong. All help is appreciated Smile

import random

def main():

    

    # Display Instructions

    instruction_list = ['Keep guessing letters until your guesses run out']

    x = 0

    y = 0

    instructions(instruction_list)

    

    #Display Puzzle String

    word = 'apple'

    blanks = (' _ '*len(word))

    display_puzzle_string(blanks)

    

    #Play Game

    num_guess = 4

    guess = ''

    guessed_letters = []

    temp = ''

    word_found = False

    correct_guess = ''

    play_game(num_guess, guessed_letters, temp, word_found, blanks, word, correct_guess)

    

    

    #Display Result

    display_result(word_found, num_guess, word)

    

    #End Game

    end_game()

	

	

def instructions(instruction_list):

    # Display Instructions

    for instruction in instruction_list:

        print(instruction)

        

        

def display_puzzle_string(blanks):

    print('The answer so far is: '+ blanks)

    return blanks



def play_game(num_guess, guessed_letters, temp, word_found, blanks, word, correct_guess):

    while num_guess>0 and word_found == False:

        guess = get_guess(num_guess)

        update_puzzle_string(blanks, temp, word, correct_guess, guess, guessed_letters)

        guessed_letters += guess

        update_num_guess(correct_guess, num_guess)

        display_updated_puzzle(temp)

        is_word_found(temp)

    return word_found

        

        

        

def get_guess(num_guess):

    guess = input('Guess a letter (You have ' + str(num_guess) + ' guesses remaining): ')    

    return guess





def update_puzzle_string(blanks, temp, word, correct_guess, guess, guessed_letters):

    for count, item in enumerate(word):

            if item == guess and item != guessed_letters:

                correct_guess = True

                temp += guess

            else:

                correct_guess = False

                temp += blanks[count]

                

            return temp

    

def update_num_guess(correct_guess,num_guess):

    if correct_guess == True:

        num_guess = num_guess

    else:

        num_guess += 1

        

    

def display_updated_puzzle(temp):

    print('The answer so far is: ' + ''.join(temp))



    

def is_word_found(temp):

    if ' _ ' in temp:

        word_found = False

    else:

        word_found = True 

    

    return word_found 

    

def display_result(word_found, num_guess, word):

    if word_found == True:

        print('Good job! You found the word' + word)

                   

    elif num_guess==0:

        print('Not quite, the correct word was ' + word+'.Better luck next time')

                   



def end_game():

    input('Press enter to end the game')

    

    



main()    

(Feb-12-2019, 07:17 PM)A1395 Wrote: this one is wrong. The temp gives an empty value. It does not update. The input letter does not seem to register with the program. What could be wrong?
Hello.

I need some help with my code. It has to be in the format I am posting. So when I run the game, it is basically supposed to keep asking for a guess until the number of guess reach 0 or until the word is found. Number of guesses start at 4 and decrease every single time the player inputs a wrong guess. The program is supposed to recognize repeated guesses of the same letter as a wrong guess. If the player guesses a letter correctly, the corresponding _ in the blanks are replaced with the letter, even if there are multiple occurrences of that letter within the word. If the player guesses all letters correctly and number of guesses is greater than 0, then the player wins. I have posted my code below. I think the problem is in the update_puzzle_string() block as it is updating the "temp" variable to give me the blanks with the word filled in. Please review it and let me know what I am doing wrong. All help is appreciated Smile

import random

def main():

    

    # Display Instructions

    instruction_list = ['Keep guessing letters until your guesses run out']

    x = 0

    y = 0

    instructions(instruction_list)

    

    #Display Puzzle String

    word = 'apple'

    blanks = (' _ '*len(word))

    display_puzzle_string(blanks)

    

    #Play Game

    num_guess = 4

    guess = ''

    guessed_letters = []

    temp = ''

    word_found = False

    correct_guess = ''

    play_game(num_guess, guessed_letters, temp, word_found, blanks, word, correct_guess)

    

    

    #Display Result

    display_result(word_found, num_guess, word)

    

    #End Game

    end_game()

	

	

def instructions(instruction_list):

    # Display Instructions

    for instruction in instruction_list:

        print(instruction)

        

        

def display_puzzle_string(blanks):

    print('The answer so far is: '+ blanks)

    return blanks



def play_game(num_guess, guessed_letters, temp, word_found, blanks, word, correct_guess):

    while num_guess>0 and word_found == False:

        guess = get_guess(num_guess)

        update_puzzle_string(blanks, temp, word, correct_guess, guess, guessed_letters)

        guessed_letters += guess

        update_num_guess(correct_guess, num_guess)

        display_updated_puzzle(temp)

        is_word_found(temp)

    return word_found

        

        

        

def get_guess(num_guess):

    guess = input('Guess a letter (You have ' + str(num_guess) + ' guesses remaining): ')    

    return guess





def update_puzzle_string(blanks, temp, word, correct_guess, guess, guessed_letters):

    for count, item in enumerate(word):

            if item == guess and item != guessed_letters:

                correct_guess = True

                temp += guess

            else:

                correct_guess = False

                temp += blanks[count]

                

            return temp

    

def update_num_guess(correct_guess,num_guess):

    if correct_guess == True:

        num_guess = num_guess

    else:

        num_guess += 1

        

    

def display_updated_puzzle(temp):

    print('The answer so far is: ' + ''.join(temp))



    

def is_word_found(temp):

    if ' _ ' in temp:

        word_found = False

    else:

        word_found = True 

    

    return word_found 

    

def display_result(word_found, num_guess, word):

    if word_found == True:

        print('Good job! You found the word' + word)

                   

    elif num_guess==0:

        print('Not quite, the correct word was ' + word+'.Better luck next time')

                   



def end_game():

    input('Press enter to end the game')

    

    



main()    
Reply


Messages In This Thread
Python hangman help - by A1395 - Feb-12-2019, 07:17 PM
RE: Python hangman help - by ichabod801 - Feb-12-2019, 08:49 PM
RE: Python hangman help - by A1395 - Feb-12-2019, 11:05 PM
RE: Python hangman help - by ichabod801 - Feb-12-2019, 11:39 PM
RE: Python hangman help - by A1395 - Feb-13-2019, 12:15 AM
RE: Python hangman help - by ichabod801 - Feb-13-2019, 03:12 AM
RE: Python hangman help - by A1395 - Feb-13-2019, 05:32 AM
RE: Python hangman help - by ichabod801 - Feb-13-2019, 03:23 PM
RE: Python hangman help - by A1395 - Feb-13-2019, 03:41 PM
RE: Python hangman help - by ichabod801 - Feb-13-2019, 03:47 PM
RE: Python hangman help - by A1395 - Feb-13-2019, 04:01 PM
RE: Python hangman help - by ichabod801 - Feb-13-2019, 04:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Hangman Game - Multiple Letters Problem t0rn 4 4,644 Jun-05-2020, 11:27 AM
Last Post: t0rn
  Hangman metro17 4 3,016 Sep-18-2019, 10:59 AM
Last Post: perfringo
  Trouble coding hangman Tay 1 2,360 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 2,398 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Hangman 2skywalkers 3 69,792 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Hangman Help. 2skywalkers 4 4,185 Jun-26-2018, 02:49 AM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 12,070 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Simple Hangman Game Issue andrew95 2 4,335 Apr-02-2018, 02:24 PM
Last Post: andrew95
  Designing Hangman game spacetimeguy 2 5,120 Feb-02-2018, 08:55 PM
Last Post: spacetimeguy
  TKinter Hangman Game Bumble 1 20,472 Jul-19-2017, 06:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020