[英]Python 3 - Having 2 items as 1 index in a list
我是 python 新手,我正在嘗試構建一個測驗。 所以其中一個問題包含 2 個答案,但我不知道如何接受這兩個答案。 如圖所示,我希望“這個”和“那個”都可以作為答案。 有沒有辦法做到這一點? 提前致謝!
questions_asked = [
"Q1",
"Q2",
]
answers = [
"Answer",
"This" or "That"
]
def run_question():
score = 0
index = 0
for question in questions_asked:
if index < len(questions_asked):
answer = input(questions_asked[index]).lower()
if answer == answers[index].lower():
score += 1
index += 1
print("{score} out of 2".format(score=score))
run_question()
你應該改變你的數據結構。 而不是answers: List[str]
,你應該使用answers: List[set]
answers = [{"one answer"}, {"another answer"}, {"a couple", "correct answers"}]
然后你可以檢查它:
expected = answers[i] # however you're doing this -- I'd probably zip it together
# with questions, but YMMV
if user_answer in expected:
# correct
請注意,您的循環可以大大簡化:
score = 0
for question, expected_answers in zip(questions_asked, answers):
user_answer = input(question).lower()
if user_answer in expected_answers:
score += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.