Python Forum

Full Version: A-Z Quiz Task
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Folks,

I'm currently learning with the edX Introductory course. One of the practice tasks set was to create an A-Z quiz in which you create a function, check_guess(), where the user is asked to input a letter and nudged in the right direction if incorrect. the next task is this:
--

Letter Guess
create letter_guess() function that gives user 3 guesses


takes a letter character argument for the answer letter
  • gets user input for letter guess
    calls check_guess() with answer and guess
    End letter_guess if
    check_guess() equals True, return True
    or after 3 failed attempts, return False
# [ ] create letter_guess() function, call the function to test
--

So, I altered check_guess() to return values-- This is one of several attempts, but I'm stuck:

letter = "p"
guess_num = 0

def check_guess(letter, guess):
    if guess > letter:
        return("letter is lower")
    elif guess < letter:
        return("letter is higher")
    elif guess == letter:
        return("correct")
    else:
        return('invalid input')
    
def letter_guess(letter, guess, guess_num):
    guess = input("guess a letter: ").lower()
    check_guess(letter, guess)
    if check_guess(letter, guess) == "correct":
        print("YOU WIN")
    elif guess_num == 3:
        print("GAME OVER")
    else:
        print(check_guess(letter, guess))
        guess_num += 1
        letter_guess(letter, guess, guess_num)
        
letter_guess(letter, guess, guess_num)
Error:
NameError Traceback (most recent call last) <ipython-input-10-7915ce883260> in <module>() 24 letter_guess(letter, guess, guess_num) 25 ---> 26 letter_guess(letter, guess, guess_num) NameError: name 'guess' is not defined
With what I've learnt so far I can't see how to introduce this 3 strikes option into the code. Any pointers would be much appreciated, I'm very new to programming and don't know where to go from here..!

Thanks
So, as said, check_guess is supposed to return True or False, not strings.
Also in order to give the player three guesses you will need a loop in your second function.
Finally, you don't really need to pass arguments to your second function. It can keep track of guess_num inside the function and passing it guess has no meaning. You can pass it the letter if you want but probably better to have it randomly pick one in the function as well.
Thanks very much for the help Mekire,

letter = "p"

def check_guess(letter, guess):
    return guess in letter
    
def letter_guess(letter):
    guess_num = 0
    guess = input("guess a letter: ").lower()
    check_guess(letter, guess)
    if check_guess(letter, guess) == True:
        return True
    elif guess_num == 3:
        return False
    else:
        guess_num += 1
        letter_guess(letter)
        
letter_guess(letter)
As you can see it's been trimmed down substantially. However I'm sill faced with a couple of issues: the first is that letter_guess does not return True if a correct guess is made after the first attempt. Secondly, initialising guess_num at the start, outside of the function, throws error:
Error:
UnboundLocalError: local variable 'guess_num' referenced before assignment
whilst storing it in the function only re-initializes it as 0 every time the function is called, making the guesses infinite?
check_guess is the one that needs to return True or False:
def check_guess(letter, guess):
    if guess > letter:
        print("Letter is lower!")
    elif guess < letter:
        print("Letter is higher!")
    elif guess == letter:
        return True
    return False
For the other function. Do not call letter_guess within the body of letter_guess. Loop with a for loop or a while loop; not recursion.
Quote:Secondly, initialising guess_num at the start, outside of the function, throws error:
Yes, we are not using global variables. It does not need to be outside the function. In fact if you write the second function correctly (with a for loop), you don't need that variable at all.