Help with python project

Question:

Repl link:

Hi! I’m a beginner in Python and trying to write a code for a hangman game but I’m having trouble running it. The code will run but won’t loop and I’m not seeing what I’m doing wrong. Could someone help me fix my code?

import random

# Initial Steps to invite in the game:
print("Let's play hangman!")
print("You only have 5 tries, but I believe in you.")
print("\nGood luck!\n")


# The parameters we require to execute the game:
def main():
    words_to_guess = ["ceramic","house","after","gift","please","about","tofu","angry","mound","sauce"
                   ,"cat", "music", "paint", "choice", "move", "desk", "greatly"]
    word = random.choice(words_to_guess)
    length = len(word)
    count = 0
    limit = 5
    already_guessed = []
    display = '_' * length
    play_game = ""

# A loop to re-execute the game when the first round ends:

def play_loop(play_game):
    play_game = ""
    play_game = input("Try again? y = yes, n = no \n")
    while play_game not in ["y", "n","Y","N"]:
        play_game = input("Try again? y = yes, n = no \n")
    if play_game == "y":
        main()
    elif play_game == "n":
        print("Sore loser!")
        exit()

    # Initializing all the conditions required for the game:
def hangman(word, count, length, display, already_guessed):
    limit = 5
    already_guessed = []
    display = '_' * length
    count = 0
    guess = input("I've chosen this word: " + display + " guess another letter if you dare: \n")
    guess = guess.strip()
    if len(guess.strip()) == 0 or len(guess.strip()) >= 2 or guess <= "9":
        print("You gotta choose a letter, dummy.\n")
        hangman(word, length, count, display, already_guessed)
    elif guess in word:
        already_guessed.extend([guess])
        index = word.find(guess)
        word = word[:index] + "_" + word[index + 1:]
        display = display[:index] + guess + display[index + 1:]
        print(display + "\n")
    elif guess in already_guessed:
        print("You already used that letter.\n")
    else:
        count += 1
        if count == 1:
            print("   _____ \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Nah! Guess again\n")
        elif count == 2:
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Wrong! Guess again\n")
        elif count == 3:
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |     | \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Still wrong. Guess again\n")
        elif count == 4:
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |     | \n"
                  "  |     O \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Wow you're not good at this. Uh oh, last try.\n")
        elif count == 5:
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |     | \n"
                  "  |     O \n"
                  "  |    /|\ \n"
                  "  |    / \ \n"
                  "__|__\n")
            print("So wrong, RIP hangman. Better luck next time?\n")
            print("The word we chose was:", already_guessed, word)
            play_loop()

        if word == '_' * length:
            print("You're brilliant!! WINNER WINNER!!!")
            play_loop()

        elif count != limit:
            hangman(word, count, length, display, already_guessed)

if __name__ == "__main__":
    main()

So…
Where is the link

I pasted the code in but the link is hangman try · GitHub

hi @hayataarrass welcome to the community.

In order for others to help you it’s better to post a link to the Repl you are working on, so they can run it and see any error messages that might pop up.

You mention for example that the code runs but doesn’t loop. Looking at the code posted above I think it’s because your main subroutine doesn’t actually call any other subroutines, just sets up the words to guess, limits, etc.

okay I just posted the link in the original post as well. I’ve added hangman(word, length, count, display, already_guessed) to my main function and its running but not looping still

Hi @hayataarrass that’s not the link I asked for. Sharing a link to a github file isn’t going to allow the community to run your Repl. Can you share the link to the Repl please?

https://replit.com/@hayataarrass/hangman#main.py

does that work? sorry for wasting time, I’m new to all this

That’s ok @hayataarrass thank you for sharing.

When I run the code and enter a letter I see the following error:

image

Is this what you see too?

yeah that’s what I see and I get similar error messages while running it in vscode

Thank you. The error in red is telling you that the variable limit hasn’t been created, so line 108 in your code doesn’t know what to do.

Have a look at this short video for an explanation why this is and how to fix it.

It still doesn’t solve your looping issue, but that’s step 2.

1 Like

wow thank you so much! super helpful, I appreciate the time you took to make that video. I’m assuming now what I’m going to do is put play_game above my main() function as well. At least that’s what I’ll try now!

2 Likes

never mind, I was wrong haha. I’ll keep at it though

1 Like

That’s no problem at all @hayataarrass thank you for your kind words.

There are a lot of ways to solve this issue and I think you aren’t far away from solving it yourself (always more rewarding) but I wanted to highlight that you are passing the parameters into hangman from line 21 slightly differently from the definition on line 36. Order matters.

1 Like

where I’m at now it’s looping but only if it’s the wrong letter chosen, if it’s a correct letter the loop stops. trying to understand that at the moment

See above. You’ve nearly got it!

1 Like

damn but I changed them all so they matched! hangman(word, length, count, display, already_guessed)

and tried with hangman(word, count, length, display, already_guessed) with all parameters matching

Good. That’ll save another issue later.

The difference between the first part of the IF statement and the ELSE IF below is that only one of them calls hangman

2 Likes

if you want i can collab with you on this too help you