0
votes

I'm writing a program that administers a five-question multiple- choice quiz on global warming and calculates the number of correct answers. I created a dictionary of dictionaries first, like:

questions = \
{
    "What is the global warming controversy about?": {
        "A": "the public debate over whether global warming is occuring",
        "B": "how much global warming has occured in modern times",
        "C": "what global warming has caused",
        "D": "all of the above"
    },
    "What movie was used to publicize the controversial issue of global warming?": {
        "A": "the bitter truth",
        "B": "destruction of mankind",
        "C": "the inconvenient truth",
        "D": "the depletion"
    },
    "In what year did former Vice President Al Gore and a U.N. network of scientists share the Nobel Peace Prize?": {
        "A": "1996",
        "B": "1998",
        "C": "2003",
        "D": "2007"
    },
    "Many European countries took action to reduce greenhouse gas before what year?": {
        "A": "1985",
        "B": "1990",
        "C": "1759",
        "D": "2000"
    },
    "Who first proposed the theory that increases in greenhouse gas would lead to an increase in temperature?": {
        "A": "Svante Arrhenius",
        "B": "Niccolo Machiavelli",
        "C": "Jared Bayless",
        "D": "Jacob Thornton"
    }
}

Then the logic is as follows:

print("\nGlobal Warming Facts Quiz")
prompt = ">>> "
correct_options = ['D', 'C', 'D', 'B', 'A']
score_count = 0

for question, options in questions.items():
    print("\n", question, "\n")
    for option, answer in options.items():
        print(option, ":", answer)
    print("\nWhat's your answer?")
    choice = str(input(prompt))
    for correct_option in correct_options:
        if choice.upper() == correct_option:
            score_count += 1
print(score_count)

The problem is if I enter all correct options, I get 7 instead of 5. I tried pushing the last statement (print(score_count)) under the if statement to monitor the score count and I figured that some questions actually add 1 twice instead of just once.

1

1 Answers

5
votes

It's because of the fact that for each question, you are iterating over all correct options for all the questions rather than checking if the provided option equals the correct option for the current question only. In other words, this part is wrong:

for correct_option in correct_options:
        if choice.upper() == correct_option:
            score_count = score_count + 1

Try this instead:

print("\nGlobal Warming Facts Quiz")
prompt = ">>> "
correct_options = ['D', 'C', 'D', 'B', 'A']
score_count = 0

for correct_option, (question, options) in zip(correct_options, questions.items()):
    print("\n", question, "\n")
    for option, answer in options.items():
        print(option, ":", answer)
    print("\nWhat's your answer?")
    choice = str(input(prompt))
    if choice.upper() == correct_option:
        score_count = score_count + 1
print(score_count)