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





def main():

    

    # Display Instructions

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

    instructions(instruction_list)

    

    #Display Puzzle String

    word = 'apple'

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

    display_puzzle_string(blanks)

    

    #Play Game

    num_guess = 4

    guess = ''

    guessed_letters = []

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

    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:

        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)

        display_updated_puzzle(new_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(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

            else:

                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

    else:

        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

    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()    
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?
Reply
#12
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 - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Hangman Game - Multiple Letters Problem t0rn 4 4,624 Jun-05-2020, 11:27 AM
Last Post: t0rn
  Hangman metro17 4 3,005 Sep-18-2019, 10:59 AM
Last Post: perfringo
  Trouble coding hangman Tay 1 2,351 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 2,392 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Hangman 2skywalkers 3 68,886 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Hangman Help. 2skywalkers 4 4,172 Jun-26-2018, 02:49 AM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 12,057 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Simple Hangman Game Issue andrew95 2 4,327 Apr-02-2018, 02:24 PM
Last Post: andrew95
  Designing Hangman game spacetimeguy 2 5,111 Feb-02-2018, 08:55 PM
Last Post: spacetimeguy
  TKinter Hangman Game Bumble 1 20,441 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