-1

I am making a quiz in python that has all the questions and answers in a dictionary. Everything works well but the problem is that I don't know how to put or in a dictionary. I tried to make it that if the user types "7" or "seven" (as the answer) the code prints correct but it doesn't. What happens is that if the user types 7 it says correct but if the user types seven (in letters) it says incorrect although seven is correct. I have done research on this but I couldn't find anything.

Here's the code. The dictionary variable is called Question_list. Also take note that the questions in the dictionary are on the left side of the : and the answers are on the right side of the :

import random

Question_list = {
   "How many days are there in a year?":"365" or "three hundred and sixty five",
   "How many hours are there in a day?":"24" or "twenty four", 
   "How many days are there in a week?":"7" or "seven"
   } 


#The part which asks the questions and verifies if the answer the user does is correct or incorrect. It also randomizes the questions
score = 0
question = list (Question_list.keys())
#input answer here 
while True:
    if not question:
        break
    ques = random.choice(question)
    print(ques)
    while True:
        answer = input('Answer ' )
        # if correct, moves onto next question
        if answer.lower() == Question_list[ques]:
            print("Correct Answer")
            break
        else:
            #if wrong, Asks the same question again
            print("Wrong Answer, try again")
    question.remove(ques)
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Make the possible answers a *list*, and check whether the answer is correct with `answer in answer_list`…! – deceze Aug 18 '21 at 12:51
  • `"7" or "seven"` evaluates to `"7"`. If you want to store multiple values against a key in your dictionary, consider using a list or a set. – khelwood Aug 18 '21 at 12:52
  • @schwobaseggl that's not really an appropriate dupe... – MattDMo Aug 18 '21 at 12:55
  • @MattDMo Maybe not an exact dupe. But I think all the concepts needed to answer question are shown there. – user2390182 Aug 18 '21 at 13:02

2 Answers2

1

Use a tuple or list or set of correct answers for each question. For example:

questions = {
    'Some question?': {'42', 'forty two'},
    ...
}

for question, answers in questions.items():
    answer = input(question)
    if answer.lower() in answers:
        ...
Iguananaut
  • 21,810
  • 5
  • 50
  • 63
  • @deceze Thank you, edited. As someone who's been using Python since 2.3 I still often forget, out of habit, that those exist now ^^ – Iguananaut Aug 18 '21 at 12:55
1

Use a list:

Question_list = {
   "How many days are there in a year?": ["365", "three hundred and sixty five"],
   "How many hours are there in a day?": ["24", "twenty four"], 
   "How many days are there in a week?": ["7", "seven"]
   } 

...

        if answer.lower() in Question_list[ques]:
MattDMo
  • 100,794
  • 21
  • 241
  • 231