0

I'm writing a function to see if a user's input is in a list, if it is the users input is removed and they are asked to guess again until they quit.

I want to count the number of correct guesses and print it after the function is called, for some reason, regardless of the number of correct guesses it prints zero. What have I done wrong? (Code and example below)

# [ ] Complete Foot Bones Quiz
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform","intermediate cuneiform", "medial cuneiform"]
guess =''
already_guessed = []

def foot_bone_quiz(guess, foot_bones):

    correct_guess = 0

    while True:

        guess = input("Guess a foot bone, enter q to quit. ")


        if guess.lower() in already_guessed:
            print("You already guessed that!")


        elif guess != 'q':

            for bone in foot_bones:

                if bone.lower() == guess.lower():
                    correct_guess = correct_guess + 1
                    print("Yes", guess, "is a bone in the foot!")
                    already_guessed.append(guess.lower())
                    foot_bones.remove(guess.lower())


                else:
                    pass


        elif guess == 'q':
            print('finished')
            break

        else:
            print("Invalid Entry")


    return already_guessed, correct_guess            

foot_bone_quiz(guess, foot_bones)
print("You had", correct_guess, "correct guesses. You guessed: ", already_guessed)

Example output:

Guess a foot bone, enter q to quit. navicular

Yes navicular is a bone in the foot!

Guess a foot bone, enter q to quit. talus

Yes talus is a bone in the foot!

Guess a foot bone, enter q to quit. q
finished

You had 0 correct guesses. You guessed:  ['navicular', 'talus']

3 Answers 3

2

It seems like you do not save your returned correct_guess from method foot_bone_quiz.

Try the following:

already_guessed, correct_guess = foot_bone_quiz(guess, foot_bones)
print("You had", correct_guess, "correct guesses. You guessed: ", already_guessed)

already_guessed was being printed correctly because it was initialized outside of the foot_bone_quiz method.

Sign up to request clarification or add additional context in comments.

Comments

1

Upon actually running your code, I got this output

Guess a foot bone, enter q to quit. navicular
Yes navicular is a bone in the foot!
Guess a foot bone, enter q to quit. talus
Yes talus is a bone in the foot!
Guess a foot bone, enter q to quit. q
finished
Traceback (most recent call last):
  File "F:\Documents\Python\stack.py", line 45, in <module>
    print("You had", correct_guess, "correct guesses. You guessed: ", already_guessed)
NameError: name 'correct_guess' is not defined

This is as expected, the name correct_guess is never defined outside of the foot_bone_quiz function scope. It seems maybe you are unfamiliar with variable scope in python and how return values work

Your quiz function returns 2 values, yet you never use these returned values when the function is invoked on the 2nd last line. Also, the already_guessed variable is defined outside of the function anyways so returning it is kind of redundant. Finally, the guess argument taken by your quiz function is completely redundant because you re-assign its value to an input before ever reading what was passed originally.

Here are some suggested changes to your program:

  • remove the already_guessed = and guess = assignments from the top of your program.
  • remove the guess parameter from your quiz function
  • put the already_guessed = [] assignment inside the quiz function at the beginning
  • when calling your quiz method at the end, assign the resulting values to variables to then be used in the print (already_guessed, correct_guess = foot_bone_quiz(foot_bones)). Note that the guess argument was removed as stated before

Here's the output I get after these changes

Guess a foot bone, enter q to quit. navicular
Yes navicular is a bone in the foot!
Guess a foot bone, enter q to quit. talus
Yes talus is a bone in the foot!
Guess a foot bone, enter q to quit. q
finished
You had 2 correct guesses. You guessed:  ['navicular', 'talus']

2 Comments

Thanks, I read through your link and went back over my course notes, you were right, I'd misunderstood the use of function arguments/parameters. I tried all your points but couldn't understand why point 4 works, so I used points 1-3 and left correct_guess as a local variable but moved the print statement inside the function and got the desired output! Going to keep reading up on functions to see if I can get my head round point 4,
@dnaylor93 when returning a value in a python function, that value is then passed along so it can be used wherever the function may be called. My corrections to your program were under the assumption that the quiz function should simply return the results of the quiz, then the code actually responsible for printing that information would be separate (the last line of the program). What you have ended up doing is a bit different, where the quiz function is responsible for getting the quiz results and printing them. In this small program, both choices have the same result, but it is a choice
-1
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
          "intermediate cuneiform", "medial cuneiform"]
correct_guess = 0
already_guessed = []


def foot_bone_quiz(foot_bones1):
    global correct_guess
    global already_guessed
    already_guessed = []
    correct_guess = 0
    while True:
        guess1 = input("Guess a foot bone, enter q to quit. ")
        if guess1.lower() in already_guessed:
            print("You already guessed that!")
        elif guess1 != 'q':
            for bone in foot_bones1:
                if bone.lower() == guess1.lower():
                    correct_guess += 1
                    print("Yes", guess1, "is a bone in the foot!")
                    already_guessed.append(guess1.lower())
                    foot_bones1.remove(guess1.lower())

                else:
                    pass
        elif guess1 == 'q':
            print('finished')
            break

        else:
            print("Invalid Entry")
    return already_guessed, correct_guess

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.