Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python的多项选择题测验_Python - Fatal编程技术网

使用Python的多项选择题测验

使用Python的多项选择题测验,python,Python,我正在写一个程序来管理一个五个问题的多个问题- 关于全球变暖的选择测验和计算数字 正确答案的数量。 我首先创建了一本词典,如: questions = \ { "What is the global warming controversy about?": { "A": "the public debate over whether global warming is occuring", "B": "how much global warming has

我正在写一个程序来管理一个五个问题的多个问题- 关于全球变暖的选择测验和计算数字 正确答案的数量。 我首先创建了一本词典,如:

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"
    }
}
然后,逻辑如下:

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)

问题是如果我输入了所有正确的选项,我得到的是7而不是5。我尝试将最后一条语句(print(score\u count))放在if语句下,以监控分数计数,我发现有些问题实际上增加了1两次,而不是一次。

这是因为对于每个问题,您正在对所有问题的所有正确选项进行迭代,而不是仅检查提供的选项是否等于当前问题的正确选项。换句话说,这部分是错误的:

for correct_option in correct_options:
        if choice.upper() == correct_option:
            score_count = score_count + 1
请尝试以下方法:

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)

这是因为,对于每个问题,您都在迭代所有问题的所有正确选项,而不是只检查提供的选项是否等于当前问题的正确选项。换句话说,这部分是错误的:

for correct_option in correct_options:
        if choice.upper() == correct_option:
            score_count = score_count + 1
请尝试以下方法:

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)