1

I have been given a basic python problem that requires me to make a simple addition quiz. However, I cannot seem to return my count variable which is supposed to update the number of correct questions the user has answered, which makes it stuck at 0. I have tried defining the variable count in every function containing it as an argument but still does not work. Say if the user were to answer 4 questions and got 3 correct, it would display it as "You have answered 4 questions with 3 correct", but instead it displays "You have answered 4 questions with 0 correct".

1
  • in all of functions that use count, you set count=0 even though the function already takes it in as a parameter. I think you are overwriting count causing it to be reset to 0 for every new arithmetic problem generated? Commented Apr 9, 2019 at 23:01

5 Answers 5

2

Every time your check_solution and menu_optionfunctions get called, you initialize count = 0. This means every time the user requests another question, count gets reset to 0, twice. You're going to want to remove those count = 0 calls, and you also want to capture your updates to count within menu_option. Your final program should look something like this:

import random

def get_user_input():
    count = 0
    user_input = int(input("Enter 1 to play or press 5 to exit: "))
    while user_input > 5 or user_input <= 0:
        user_input = int(input("Invalid menu option. Try again: "))
        menu_option(user_input, count)

        if user_input == "5":
            print("Exit!")

    return user_input

def get_user_solution(problem):
    answer = int(input(problem))
    return answer

def check_solution(user_solution, solution, count):
    curr_count = count
    if user_solution == solution:
        curr_count += 1
        print("Correct.")

    else:
        print("Incorrect.")
    print(curr_count)
    return curr_count

def menu_option(index, count):
    if index == 1:
        num1 = random.randrange(1, 21)
        num2 = random.randrange(1, 21)
        randsum = num1 + num2
        problem = str(num1) + " " + "+" + " " + str(num2) + " " + "=" + " "
        user_answer = get_user_solution(problem)
        count = check_solution(user_answer, randsum, count) # count returned by check_solution is now being captured by count, which will update your count variable to the correct value

    return count

def display_result(total, correct):
    if total == 0:
        print("You answered 0 questions with 0 correct.")
        print("Your score is 0%. Thank you.")
    else:
        score = round((correct / total) * 100, 2)
        print("You answered", total, "questions with", correct, "correct.")
        print("Your score is", str(score) + "%.")

def main():
    option = get_user_input()
    total = 0
    correct = 0
    while option != 5:
        total = total + 1
        correct = menu_option(option, correct)
        option = get_user_input()

    print("Exiting.")
    display_result(total, correct)

main()

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

Comments

1

You need catch the return from check_solution(user_answer, randsum, count) and return that count

Comments

1

As the comment stated, you are initializing count to 0 every time your check_solution or menu_option is called.

It looks like you want to use count = count the variable being passed to your function.

Just a quick edit:

You actually don't need to return count. In Python, variables are passed by reference so your count will get updated as long as it's being passed to your functions.

Comments

0

You have the option of initializing count to 0 before all functions, thus creating a global variable. Then you won't need to declare it on any function nor pass it as argument.

Comments

0

This is a culmination of several errors in logic.

  • You give count to functions as input and immediately overwrite it.

    • I would instead say def menu_option(index, count=0):. This will set count=0 if no variable is supplied (creating a default), otherwise it will set count as whatever you pass into the function
  • Your check_solution() function returns a number, but when you call it with check_solution(user_answer, randsum, count) you never assign this returned value to anything/use it again.

    • You can assign this to a variable (say output) and then return output instead of return count

Fixing these still doesn't fully solve the problem, but gets a little bit closer (now it gets stuck on "you answered x questions with 1 correct"):

import random

def get_user_input(count = 0):
    user_input = int(input("Enter 1 to play or press 5 to exit: "))
    while user_input > 5 or user_input <= 0:
        user_input = int(input("Invalid menu option. Try again: "))
        menu_option(user_input, count)

        if user_input == "5":
            print("Exit!")

    return user_input

def get_user_solution(problem):
    answer = int(input(problem))
    return answer

def check_solution(user_solution, solution, count):
    count = 0
    if user_solution == solution:
        count += 1
        print("Correct.")

    else:
        print("Incorrect.")

    return count

def menu_option(index, count=0):
    if index == 1:
        num1 = random.randrange(1, 21)
        num2 = random.randrange(1, 21)
        randsum = num1 + num2
        problem = str(num1) + " " + "+" + " " + str(num2) + " " + "=" + " "
        user_answer = get_user_solution(problem)
        output = check_solution(user_answer, randsum, count)
    return output

def display_result(total, correct):
    if total == 0:
        print("You answered 0 questions with 0 correct.")
        print("Your score is 0%. Thank you.")
    else:
        score = round((correct / total) * 100, 2)
        print("You answered", total, "questions with", correct, "correct.")
        print("Your score is", str(score) + "%.")

def main():
    option = get_user_input()
    total = 0
    correct = 0
    while option != 5:
        total += 1
        correct = menu_option(option, correct)
        option = get_user_input()

    print("Exiting.")
    display_result(total, correct)

main()

I think a more simplistic approach would look something like:

import random

def generate_question():
    num1 = random.randint(1, 25)
    num2 = random.randint(1, 25)
    question = '{} + {} = '.format(num1, num2)
    answer = num1 + num2
    return question, answer

def main():
    correct = 0
    total = 0
    option = True
    while option != '5':
        total += 1
        question, answer = generate_question()
        user_guess = int(input(question))
        if user_guess == answer:
            print('Correct.')
            correct += 1
        else:
            print('Incorrect.')
        option = input("Enter 5 to exit, or anything else to play again")
    print('You answered {} questions with {} correct'.format(total, correct))

main()

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.