I am new to python and I am trying to build a quiz thingy. And so one of the questions contain 2 answers to it but I cant figure out how to accept both of the answers. As shown in the pic, I want to have both 'this' and 'that' to be acceptable as an answer. Is there a way to do it? Thanks in advance!
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()
You should change your data structure. Rather than a answers: List[str]
, you should use answers: List[set]
answers = [{"one answer"}, {"another answer"}, {"a couple", "correct answers"}]
Then you can check it with:
expected = answers[i] # however you're doing this -- I'd probably zip it together
# with questions, but YMMV
if user_answer in expected:
# correct
Note that your loop can be greatly simplified:
score = 0
for question, expected_answers in zip(questions_asked, answers):
user_answer = input(question).lower()
if user_answer in expected_answers:
score += 1
Or make your "answers" as dictionaries.
dict = {'answer1': 'this', 'answer2': 'that', 'answer3': 'None'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.