2

am trying to teach myself Python and am building myself a basic hangman game. I've got a little way but now I'm stuck.

I've tried to put the bulk of the 'counter' for guessing in one function and then reference another function to actually work out whether the guess is correct or not.

So here is my counter function:

def game(generated_word):
    hangman_lst = []
    guess_num = 0
    for i in range(0, len(generated_word)):
        hangman_lst.append("__")
    print(" ".join(hangman_lst))

    while "__" in hangman_lst:
        if guess_num == 0:
            l = input("This is your first guess. Guess a letter!")
            guess(l, random_word_stripped, hangman_lst, guess_num)
        elif guess_num == 9:
            print("Sorry - you lose!")
            print("The word was " + str(random_word_stripped))
            break
        else:
            l = input("This is guess #" + str(guess_num + 1) + ":")
            guess(l, random_word_stripped, hangman_lst, guess_num)

    if "__" not in hangman_lst:
        print("**Ta-da!** You're a winner!")

and here is my function to work out whether the guess is correct or not:

def guess(ltr, word, lst, try_num):
    upper_ltr = ltr.upper()
    if len(upper_ltr) > 1:
        print("That's more than 1 letter, try again:")
        print(" ".join(lst))
    elif len(upper_ltr) < 1:
        print("You didn't enter a letter, try again:")
        print(" ".join(lst))
    elif upper_ltr in lst:
        print("You already guessed that letter, try again:")
        print(" ".join(lst))
    elif upper_ltr not in word:
        print("Sorry, that's incorrect. Try again.")
        print(" ".join(lst))
        try_num += 1
        return try_num
    else:
        for n in range(0, len(word)):
            if word[n] == upper_ltr:
                lst[n] = upper_ltr
        print("Nice job. That's one. You get a bonus guess.")
        print(" ".join(lst))
        return lst

So it kind of works, in that the hangman word is updated with the letters as they are guessed. But I can't get the counter to work - guess_num seems to always stay on 0 and so it always loops through this part:

if guess_num == 0:
    l = input("This is your first guess. Guess a letter!")
    guess(l, random_word_stripped, hangman_lst, guess_num)

But, it seems as if lst is returned ok from the guess function, so why isn't try_num?

Cheers!

2
  • You've probably misunderstood what return does, considering that you never use guess's return value. Commented May 10, 2016 at 23:08
  • Yes, more than likely :-) I'm kind of making stuff up as I go along. But sometimes I guess I get the wrong end of the stick... Commented May 10, 2016 at 23:10

2 Answers 2

1

You need to increment guess_num. Adding guess_num = guess_num + 1 at the end of the while loop would fix this, but you only want guess_num incremented on a bad guess, so you would do something like this. (I haven't tested this)

while "__" in hangman_lst:
    if guess_num == 0:
        l = input("This is your first guess. Guess a letter!")
        if guess(l, random_word_stripped, hangman_lst, guess_num):
            print("good guess")
        else:
            guess_num += 1
    elif guess_num == 9:
        print("Sorry - you lose!")
        print("The word was " + str(random_word_stripped))
        break
    else:
        l = input("This is guess #" + str(guess_num + 1) + ":")
        if guess(l, random_word_stripped, hangman_lst, guess_num):
            print("good guess")
        else:
            guess_num += 1

And then in your guess function you need to add the line return True or return False to return the appropriate True/False value depending on if there guess was valid.

So the part of the guess function that goes

if len(upper_ltr) > 1:
    print("That's more than 1 letter, try again:")
    print(" ".join(lst))

Will need to be changed to

if len(upper_ltr) > 1:
    print("That's more than 1 letter, try again:")
    print(" ".join(lst))
    return False

And then you will do this for each of the if conditions. As soon as python sees a return statement it will exit the function and will return whatever you tell it to return which in our case will be either True or False.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh cool, thanks - this is helpful! I did have try incrementing inside the while loop before but couldn't figure out how to handle only incrementing on a bad guess. But I didn't think about returning the True/False value like you suggest - will give that a shot!
Hope it works out for you. If it does, it would be great if you went ahead and upvoted my answer ;)
Thanks - I did upvote but unfortunately my rep isn't high enough for it to count :-(. I basically did a similar method to what you suggested only I used a flag = # instead of just True and False as I had more than two outcomes that I wanted to use. Probably not the prettiest code, but it's working well!
1

When you use the return keyword, that indicates that you're trying to a give a computed value back to the function's caller. Since with return you are giving the caller back a value, you would expect to assign that value to something once the function is completed...something you aren't doing.

In case you don't understand, here is an example...

Say I have my main function calling a function called add(), and say I want this function to compute the value of two integers being passed in. Then, I would have to return the computed value back to the caller for further use.

def main():
    a = 5
    b = 20
    sum = add(a, b)
    print(sum)

def add(num1, num2):
    return num1 + num2

THEN, this code would print the value of a + b, or in this case, 25.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.