python - 对python来说相对较新,我正在尝试编写一个测验,要求用户输入多个输入并用答案字典检查它们
问题描述
我知道我需要另一个 for 循环或 while 循环才能遍历 answer_inputs 中的列表,但我遇到了麻烦。我知道这是一个简短的答案测试,但每个问题只有几个答案。
questions = ["What are the three elements of a firing team?",
"What command level does a FiST belong to and what are its elements?",
"What Command level does a FSCC belong to and what are its elements?",
"What are the Fire Support Planning Phases?",
"What are the two general types of targets?"]
answer_inputs = [{"1.","\n2.","\n3.\n"},
{"Level:","\n1.","\n2.","\n3.","\n4.","\n5.\n"},
{"Level:","\n1.","\n2.","\n3.","\n4.","\n5.\n"},
{"1.\n2.\n3.\n"},
{"1.\n2.\n"}]
one = "observer" or "FDC" or "Howitzer" or "M777" or "155mm"
two = "Company FiST Leader" or "Forward Air Controller" or "FAC" or "Fire Support officer" or "Artillery FO" or "81mm Mortar Observer (FO)" or "Naval Gun-fire spotter team"
three = "Fire Support Coordinator" or "Tactical Air control Party" or "TACP" or "Liaison section" or "81mm mortar section" or "Shore Fire control Party"
four = "preperatory" or "conduct" or "consolidation"
five = "linear" or "point"
correct_choices = [{one,one,one},
{"Company", two, two, two, two, two},
{"Battalion", three, three, three, three, three},
{four, four, four},
{"linear" or "point","linear" or "point"}]
answers = ["Observer\nFDC\nHowitzer or M777 or 155mm",
"Level:Company\n1.Company FiST Leader\n2.Forward Air Controller (FAC)\n3.Fire Support officer (artillery FO)\n4.81mm Mortar Observer (FO)\n5.Naval Gun-fire spotter team\n:",
"Level:Battalion\n1.Fire Support Coordinator\n2.\n3.\n4.\n5.\n:",
"preperatory, conduct and consolidation",
"linear and point"]
def quiz():
score = 0
for question, choices, correct_choice, answer in zip(questions, answer_inputs, correct_choices, answers):
print(question)
user_answer = input(choices).lower().rstrip("s")
if user_answer in correct_choices:
print("Correct")
score += 1
else:
print("Incorrect",print("_"*10), answer)
print(score, "out of", len(questions), "that is", float(score / len(questions)) * 100, "%")
if __name__ == "__main__":
quiz()
解决方案
这是我为多个正确答案拼凑起来的快速解决方案。如果您真的想硬编码,可以跳过测验生成部分,但我建议您将问题存储在 txt 或 json 等文件中,以便稍后阅读。
import random
questions = []
answers = []
options = []
# questions and answers generation
while True:
question = str(input("Enter a question: (type done to finish) "))
if question == "done":
break
questions.append(question)
answer = ""
nested_answers = []
while True:
answer = str(input("Enter a correct answer: (type done to finish) "))
if answer == "done":
answers.append(nested_answers)
break
nested_answers.append(answer)
nested_options = []
while True:
option = str(input("Enter a (wrong) option: (type done to finish) "))
if option == "done":
options.append(nested_options)
break
nested_options.append(option)
score = 0
for i in range(len(questions)):
question = questions[i]
print("\nQuestion " + str(i + 1) + ": " + question)
combined_options = options[i] + answers[i]
random.shuffle(combined_options)
print("Options")
for count, item in enumerate(combined_options, 1):
print(str(count) + ": " + item)
user_answer = int(input("Enter your answer (number): "))
user_choice = combined_options[user_answer - 1]
if user_choice in answers[i]:
print("Correct")
score += 1
else:
print("Wrong :(")
print("Final score: " + str(score))
样品运行:
Enter a question: (type done to finish) which of these are fruits
Enter a correct answer: (type done to finish) apple
Enter a correct answer: (type done to finish) pear
Enter a correct answer: (type done to finish) orange
Enter a correct answer: (type done to finish) done
Enter a (wrong) option: (type done to finish) lettuce
Enter a (wrong) option: (type done to finish) broccoli
Enter a (wrong) option: (type done to finish) done
Enter a question: (type done to finish) which of these are vegetables
Enter a correct answer: (type done to finish) lettuce
Enter a correct answer: (type done to finish) cabbage
Enter a correct answer: (type done to finish) done
Enter a (wrong) option: (type done to finish) cat
Enter a (wrong) option: (type done to finish) done
Enter a question: (type done to finish) done
Question 1: which of these are fruits
Options
1: apple
2: lettuce
3: broccoli
4: pear
5: orange
Enter your answer (number): 1
Correct
Question 2: which of these are vegetables
Options 1: cat
2: cabbage
3: lettuce
Enter your answer (number): 2
Correct
Final score: 2
推荐阅读
- python - 如何计算两个高斯混合的单个均值、标准差、相对比例?
- javascript - string.slice(0, -1) 如何处理 javascript 中的字符串?
- c# - 如何将单词附加到带有双引号的特定字符串?
- django - django 数字海洋 git
- android - 一旦活动从另一个功能开始,如何共享数据?
- schema.org - 我应该将哪种 Schema.org 类型用于数字商品服务?
- c++ - 如何在 clion(bazel 项目)中设置 c++ 标准?
- java - Marshmallow 每次都要求存储许可,即使一旦给予
- android - androidx.constraintlayout.widget.ConstraintLayout:ProgressBar 不显示在 MaterialButton 上方
- python - 如何使用 Zipline 实施 24 小时交易日历。无法使用抽象方法实例化抽象类 {}