I just started coding in Python a few weeks ago and I'm building small projects to build my knowledge. Today I started building a Hangman but I've been stuck here for 2H+.
My CODE:
#Imports-----------------------------------------------
from time import sleep
from random import choice
#Lists------------------------------------------------
wordList = ["Mortgage", "Marsupial", "Bloodshed", "Adventure", "Chronological", "Issue"]
#Functions---------------------------------------------
def wordGuessing():
NumberOfMisses = 0
WordOrLetter = input("Do you want to guess the word or a letter? Type 'Word' or 'Letter?'")
if WordOrLetter == "Word": #------------------------------------------------------------------------------If guessing Word
guessingWord = input("Try guessing the word! (First letter is Uppercase, the rest is lowercase)")
if guessingWord == chosenWord: #--------------------------------------------------If correct
print("Hurray! You got it!")
else: #--------------------------------------------------If wrong
print(NumberOfMisses)
if NumberOfMisses == 6:
print("Oh no! Too many mistake! Game Over!")
exit()
else:
print("Oops. You didn't guess correctly! ")
print(NumberOfMisses)
NumberOfMisses += 1
wordGuessing()
elif WordOrLetter == "Letter": #--------------------------------------------------If guessing Letter
guess = input("Guess a letter! (Type in lowercase)")
print("You chose the letter " + guess + "!")
sleep(1)
if guess in chosenWord:
print("You got it right!")
else:
print("Oops! That letter isn't in the word!")
print(NumberOfMisses)
NumberOfMisses += 1
print(NumberOfMisses)
wordGuessing()
#CODE--------------------------------------------------
print("Hi! Welcome to the Hangman Python Edition")
chosenWord = choice(wordList)
chosenWordLength = len(chosenWord)
print(chosenWord) #Remove later----------------------------------------REMOVE LATER
print("I have chosen a " + str(chosenWordLength) + " letter word!")
wordGuessing()
My particular problem comes with the NumberOfMisses
variable.
In the given code, it works well but since I restart the function to keep playing, it resets to 0
.
If I move the variable outside of the function, whether next to imports or in CODE, it gives me the error in the Title.
I've tried global, I've checked the control flow, I've tried everything I can and truly cannot comprehend the problem.
The print(NumberOfMisses)
is intentional, to see if there's an update.
Can anyone help me out?