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?
break
is used to exit loops, such as for
or while
. Here your "loop" is actually recursion, so instead you want to use return
which makes the function return a value and stop, in our case we will simply return nothing, so None
. This should work:
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 # exit recursion
print(displayed_word)
if guess not in secret_word:
miss_count[0] += 1
print(hangman[miss_count[0]])
if miss_count[0] == len(hangman) -1:
return # exit recursion
hangman_start()
hangman_start()