Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python hangman help
import random

def main():


    # Display Instructions

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



    #Display Puzzle String

    word = 'apple'

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



    #Play Game

    num_guess = 4

    guess = ''

    guessed_letters = []

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

    word_found = False


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



    #Display Result

    display_result(word_found, num_guess, word)


    #End Game




def instructions(instruction_list):

    # Display Instructions

    for instruction in instruction_list:




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:

        correct_guess = False

        guess = get_guess(num_guess)

        new_temp, correct_guess = update_puzzle_string(temp, word, correct_guess, guess, guessed_letters)

        guessed_letters += guess

        num_guess = update_num_guess(correct_guess, num_guess)



    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(temp, word, correct_guess, guess, guessed_letters):

    new_temp = ''

    for count, item in enumerate(word):

            if item == guess and item not in guessed_letters:

                correct_guess = True

                new_temp += guess


                new_temp += temp[count]


    return new_temp, correct_guess


def update_num_guess(correct_guess,num_guess):

    if correct_guess == True:

        num_guess = num_guess


        num_guess += -1


    return num_guess



def display_updated_puzzle(new_temp):

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


def is_word_found(temp):

    if '_' in temp:

        word_found = False


        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')


New temp is being updated each turn but it is still being refreshed. Not elongated though. So I want to input a and get a____, then p and get app___. Right now I am getting a_____, and if I input p I get _pp___. What can I do to make the letters accumulate and completely replace all the blanks in the new_temp if all letters are guessed currently?
In play_game, you are assigning the result of update_puzzle_string to new_temp. That is lost on the next loop cycle when you call update_puzzle_string again. You want to assign to temp rather than new_temp. That way, then next time through the loop the modified version with the letters previously chosen will be sent to update_puzzle_string.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Trouble coding hangman Tay 1 217 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 222 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Hangman 2skywalkers 3 734 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Hangman Help. 2skywalkers 4 855 Jun-26-2018, 02:49 AM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 1,922 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Simple Hangman Game Issue andrew95 2 1,333 Apr-02-2018, 02:24 PM
Last Post: andrew95
  Designing Hangman game spacetimeguy 2 1,555 Feb-02-2018, 08:55 PM
Last Post: spacetimeguy
  TKinter Hangman Game Bumble 1 5,446 Jul-19-2017, 06:56 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)