1

I have been trying to make a mathimatical quiz that asks a random question then asks another random question for a range of 10 times, what my problem is is that it will ask a random question then the same question again and again and again etc.

import random

opList = ["+", "*", "-"]
numberofQuestions = 10
num1 = random.randint(0, 10)
num2 = random.randint(0, 10)
ops = random.choice(opList)
question = "%d %s %d" % (num1, ops, num2)
answer = eval(question)
totalScore = 0


childName = input("Enter your name: ")
print("Thank you for signing up for Arithmetic Quiz " + childName)
for i in range(numberofQuestions):
    reply = int(input("What is " + question + " ? "))
    if reply == answer:
        print("Correct!!")
        totalScore += 1
    else:
        print("Incorrect!!")

3 Answers 3

1

Putting these lines:

num1 = random.randint(0, 10)
num2 = random.randint(0, 10)
ops = random.choice(opList)
question = "%d %s %d" % (num1, ops, num2)
answer = eval(question)

where you have means they only evaluate once, assigning randomly-selected but static values to each variable.

You need to move these statements inside your for loop, so that they get re-evaluated every time you want new values

Sign up to request clarification or add additional context in comments.

1 Comment

Small note: the question and answer will also need to be included in the loop iteration, or they will never change either.
1

The problem is you are only generating the question (and components of the question) once. My suggestion would be to generate a new function like so:

...

def question_answer():
    num1 = random.randint(0, 10)
    num2 = random.randint(0, 10)
    ops = random.choice(opList)
    question = "%d %s %d" % (num1, ops, num2)
    answer = eval(question)
    return question, answer

for i in range(numberofQuestions):
    question, answer = question_answer()
    ...

Comments

1

You aren't updating the num1, num2, ops, question and answer variables. You need to move them into the for loop. Otherwise they will always be equal to the first value they were assigned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.