How to reset stats in a Hangman game

This is the place for queries that don't fit in any of the other categories.

How to reset stats in a Hangman game

Postby lucasf09 » Tue Dec 31, 2013 1:32 pm

Hi,

I am new to Python, having been given a Raspberry Pi for Xmas, having previously dabbled in C, and at uni having used MatLab quite a lot.
From the "Programming the Raspberry Pi" book by Simon Monk I have created a Hangman Game to which I am trying to set up a restart code. I have managed to reset the lives back to 14 for a new game, but cannot clear the guessed_letters variable, hence the new games carry on with previously guessed letters, which removes the point of restarting the game altogether.
I have tried to simply reset guessed_letters to '', and also tried to set it to 0 and to '0', all of which don't work or give an error.
In MatLab I would have added a clear or clear all function just before rerunning play(), but from my searches on google there doesn't seem to be an equivalent Python function.
I'm posting the whole code for you to try it if you wish, but the restart bit is in the play() block

Code: Select all
import random

words = ['chicken', 'dog', 'cat', 'mouse', 'frog']
lives_remaining = 14
guessed_letters = ''



def play():
        word = pick_a_word()
        global lives_remaining
        global guesed_letters
        lives_remaining = 14
       
        while True:
                guess = get_guess(word)
               
                if process_guess(guess, word):
                        print('You win! Well Done!')
                        restart_game = input ('Would you like to restart? enter 1 to restart, or 2 to leave')
                        if not restart_game == '1':
                                print ('Thanks for playing')
                                break
                        else:
                               
                                play()
                                guessed_letters=''
                               
                if lives_remaining == 0:
                        print('You are Hung!')
                        print('The word was: ' + word)
                        restart_game = input ('Would you like to restart? enter 1 to restart, or 2 to leave')
                        if not restart_game == '1':
                                print ('Thanks for playing')
                                break
                        else:
                               
                                play()
                                guessed_letters=''
       
def pick_a_word():
        word_position = random.randint(0, len(words) - 1)
        return words[word_position]
       
def get_guess(word):
        print_word_with_blanks(word)
        print('Lives Remaining: ' + str(lives_remaining))
        print (guessed_letters)
        guess = input(' Guess a letter or whole word?')
        return guess
       
def print_word_with_blanks(word):
        display_word = ''
       
        for letter in word:
                if guessed_letters.find(letter) > -1:
                        # letter found
                        display_word = display_word + letter
                else:
                        # letter not found
                        display_word = display_word + '-'
        print(display_word)

def process_guess(guess, word):
        if len(guess) > 1:
            if len(guess) == len (word):
                return whole_word_guess(guess, word)
            else:
                print ('Length of Guess does not match length of Word')
        else:
                return single_letter_guess(guess, word)

                       
def whole_word_guess(guess, word):
        global lives_remaining
        if str.lower(guess)      == word:
                return True
        else:
                lives_remaining = lives_remaining - 1
                return False

def single_letter_guess(guess, word):
        global guessed_letters
        global lives_remaining
        if word.find(guess.lower()) == -1:
                # letter guess was incorrect
                lives_remaining = lives_remaining - 1
        guessed_letters = guessed_letters + str.lower(guess)
        if all_letters_guessed(word):
                return True
        return False

def all_letters_guessed(word):
        for letter in word:
                if guessed_letters.find(letter) == -1:
                        return False
        return True
       
play()


Any help would be appreciated

Thanks in advanced
Last edited by stranac on Tue Dec 31, 2013 2:32 pm, edited 1 time in total.
Reason: First post lock.
lucasf09
 
Posts: 1
Joined: Tue Dec 31, 2013 1:09 pm

Re: How to reset stats in a Hangman game

Postby metulburr » Tue Dec 31, 2013 3:26 pm

The whole code structure is the reason for the confusion. Using globals for instance is bad practice. It would be better to use a class so each function doesnt have to recieve the argument word. Plus it gets rid of the needed globals. Using classes will remove some of your code just from redundancy. For example, i attempted a quick switch of your code to classes, but after a couple minutes, i realized the whole thing needs to be rewritten essentially to use classes. So i will leave that up to you. Plus you have recursion in your play function, which in code such as this is not ideal. The better way is to return to a main function to determine whether to replay or not.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to reset stats in a Hangman game

Postby stranac » Tue Dec 31, 2013 3:27 pm

Code: Select all
play()
guessed_letters=''

You first play the game again, and then reset the letters.
If you just change the order of those lines, your code would probably work.

But please, don't use globals. There are a few good uses for them, but this is not one of those.
Create the variables inside the function and pass them around. Using a class might also be a good idea.

Some additional advice:
  • Don't recursively call your function like that. It's better to use a loop:
    Code: Select all
    restart_game = '1'
    while restart_game == '1':
        play()
        restart_game = input('Would you like to restart? enter 1 to restart, or 2 to leave')
    print('Thanks for playing')

  • No need to reinvent the wheel: random.choice()
  • You can print multiple values at once. No need for string concatenation:
    Code: Select all
    print('Lives Remaining:', lives_remaining)

  • Code: Select all
    if guessed_letters.find(letter) > -1:

    should be written as
    Code: Select all
    if letter in guessed_letters:

  • Code: Select all
    if all_letters_guessed(word):
        return True
    return False

    is the same as
    Code: Select all
    return all_letters_guessed(word)

  • It is a strong convention to use 4 spaces for indentation.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], mdovey and 6 guests