Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use an elif (or something else) to break out of this function upon a certain condition being met

displayed_word = [" ", " ", " ", " "]
hangman = [":(", "-I", "-<", "you died!"]
guess_count = 0
secret_word = ["t", "e", "s", "t"]
miss_count = [-1]

def congratulations():
    print("congratulations, you solved it")

def hangman_start():
    guess = input("guess your letter:")
    secret_word = ["t", "e", "s", "t"]
    if guess == secret_word[0]:
        displayed_word[0] = guess
    if guess == secret_word[1]:
        displayed_word[1] = guess
    if guess == secret_word[2]:
        displayed_word[2] = guess
    if guess == secret_word[3]:
        displayed_word[3] = guess
    elif displayed_word == secret_word:
        print("congratulations")
    print(displayed_word)
    if guess not in secret_word:
        miss_count[0] += 1
        print(hangman[miss_count[0]])
    hangman_start()

hangman_start()

I have gotten my ‘hangman’ game to work the way i want it to for now, but the one thing I can’t figure out is how to have it exit upon the miss_count hitting "you died!" or upon displayed_word being the same as the secret_word. I can have it print statements at that time, but when I try to add "break" under those lines I get "break outside of loop". Is there a different command to do this?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The keyword you are looking for is return, used like so

displayed_word = [" ", " ", " ", " "]
hangman = [":(", "-I", "-<", "you died!"]
guess_count = 0
secret_word = ["t", "e", "s", "t"]
miss_count = [-1]

def congratulations():
    print("congratulations, you solved it")

def hangman_start():
    guess = input("guess your letter:")
    secret_word = ["t", "e", "s", "t"]
    if guess == secret_word[0]:
        displayed_word[0] = guess
    if guess == secret_word[1]:
        displayed_word[1] = guess
    if guess == secret_word[2]:
        displayed_word[2] = guess
    if guess == secret_word[3]:
        displayed_word[3] = guess

    elif displayed_word == secret_word:
        print("congratulations")
        return

    print(displayed_word)
    if guess not in secret_word:
        miss_count[0] += 1

        if (your condition for "you died!" and ending the game):
            return

        print(hangman[miss_count[0]])
    hangman_start()

hangman_start()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading