'Wrtiting Functions For An Existing Wordle Program

I'm writing a simple wordle program in Python. I have to add functions to the already existing code I have, but I've been getting majorly stuck. I posted what I have come up with so far. I keep getting an error with the last line that 'guess' is undefined. What can I do to fix this?

#set the word
word = 'INDEX'
WORDLENGTH = len(word)
MAXGUESS = 6
numGuess = 1
correct = False

def getWord(word, numGuess, MAXGUESS):
        while (numGuess <= MAXGUESS and not(correct)):
            guess = input('Enter a 5 letter word: ')
            guess = guess.upper()
        
        print()
        print(guess)
        break
    return guess.upper()    

getWord(word, numGuess, MAXGUESS)

def checkWord(word, guess, numGuess, WORDLENGTH):
        #check each letter. 
        #if the letter is in the word then
        #  is letter in the right position, if so print O
        #  letter in the word, wrong position, *
        #else letter not in the word, X
        ndx = 0
        while(ndx < WORDLENGTH):
            if (guess[ndx] in word):
                if (guess[ndx] == word[ndx]):
                    print('O',end='')
                else:
                    print('*',end='')
            else: 
                print('X',end='')
            
        ndx += 1
    
    print()
    
    if(guess == word):
        correct = True    
    numGuess += 1
    if(correct):
        print('You won!')
    else:
        print('Better luck next time')

checkWord(word, guess, numGuess, WORDLENGTH)


Solution 1:[1]

You aren't putting the results of getWord() in a variable, so they just vanish into the ether and guess is indeed undefined when you call checkWord() later. Try:

guess = getWord(word, numGuess, MAXGUESS)

Solution 2:[2]

You are getting the error 'guess' is undefined in the line checkWord(word, guess, numGuess, WORDLENGTH) the variable guess. Something like this should work

# set the word
word = 'INDEX'
WORDLENGTH = len(word)
MAXGUESS = 6
numGuess = 1
correct = False


def getWord(word, numGuess, MAXGUESS):
    while (numGuess <= MAXGUESS and not (correct)):
        guess = input('Enter a 5 letter word: ')
        guess = guess.upper()

        print()
        print(guess)
        break

    return guess.upper()

guess = getWord(word, numGuess, MAXGUESS)


def checkWord(word, guess, numGuess, WORDLENGTH):
    # check each letter.
    # if the letter is in the word then
    #  is letter in the right position, if so print O
    #  letter in the word, wrong position, *
    # else letter not in the word, X
    ndx = 0
    while (ndx < WORDLENGTH):
        if (guess[ndx] in word):
            if (guess[ndx] == word[ndx]):
                print('O', end='')
                break
            else:
                print('*', end='')
                break
        else:
            print('X', end='')
            break

    ndx += 1


    print()

    if (guess == word):
        correct = True
        print('You won!')
    elif (numGuess == MAXGUESS):
        print('You lost!')
    else:
        numGuess += 1
        print('Guesses left: ', MAXGUESS - numGuess)


checkWord(word, guess, numGuess, WORDLENGTH)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gray-lab
Solution 2 Flow