0

I am trying to create a math game where it randomly chooses a question from my list and gives the corresponding answer. At the moment, I can get the questions to randomise however, it gives the answer in order from the list, not the answer corresponding to the question. How do I get the answer to be correct to the random question. Here is my code.

import random

counter = 0
score = 0
incorrect = 0

name=input("What is your name?")
print("Hi",name,",welcome to your math quiz!")

questions = ["10*2","4-2","6+12","6*4","12-5","6+54","1*0","3-6","4+0","65-9"]
answers=["20","2","18","24",'7','60','0','-3','4','56']

idx_questions = list(enumerate(questions))
idx_answers = list(enumerate(answers))

random.shuffle(idx_questions)

for idxq, question in idx_questions:
    print(question)
    ans = input("What is the answer? ")
    for idxa, answer in idx_answers:
        if idxq == idxa and ans == answer:
            print("Correct")
            score=score+1
            print("Correct Answers=",score)
            print("Incorrect Answers=",incorrect)

        elif idxq == idxa and ans != answer:
            print("Incorrect", answers)
            incorrect=incorrect+1
            print("Correct Answers=",score)
            print("Incorrect Answers=",incorrect)

print("End of quiz")
print(name,"your score is",score,"out of 10")
print(score*10,"%")
Jane
  • 35
  • 5
  • 2
    Please edit your post and add the actual code, instead of linking a screenshot. This helps users reading and understanding your code, and allows copying it for testing. – Hexaholic Oct 10 '17 at 10:48

2 Answers2

0
import random

counter = 0
score = 0
incorrect = 0

questions = ["10*2","4-2","6+12","6*4"]
answers=["20","2","18","24"]

idx_questions = list(enumerate(questions))
idx_answers = list(enumerate(answers))

random.shuffle(idx_questions)

for idxq, question in idx_questions:
    print(question)
    ans = input("What is the answer? ")
    for idxa, answer in idx_answers:
        if idxq == idxa and ans == answer:
            print("Correct")
        elif idxq == idxa and ans != answer:
            print("Incorrect")
nachmr
  • 118
  • 7
  • Can you please explain further how to do this? I am new to python – Jane Oct 10 '17 at 12:30
  • Thank you! That actually works perfectly. my issue now is that when the answer is incorrect, I want to display the correct answer. However at the moment, it displays the whole list of answers. Do you have any suggestions? I have added in my current code to my question – Jane Oct 10 '17 at 22:52
  • @Jane just print in the second if the answer (without the s) variable. Please if you think that I helped you upvote the comment and, if you think that I solved your question mark it as the correct answer – nachmr Oct 11 '17 at 06:09
  • Thank you so much! – Jane Oct 11 '17 at 07:48
0

The problem is that you shuffle question list, while answers list stays the same. So now answers and questions, have a diffrent position and they doesn't match. Instead you should just generate random index and print question at that index.

random_int = random.randint(0, len(questions)-1)
print(question[random_int])

Of course you should build some mechanism to prevent the same answer to pop twice. One way is removing used questions.

del questions[random_int]
del answers[random_int]
  • This works great! Thank you. Only problem I am having is that the list index is out of range after 9 questions, even though i have my counter at 10 and random.randint at (0,10) – Jane Oct 10 '17 at 12:47
  • There is no index 10 if your list is len == 10. It is from 0 to 9. So your random.randint should be (0,9) also if you delete used answer it igets lower and lower. – Łukasz Szczesiak Oct 10 '17 at 13:01