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 :
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()