How can I randomly select a math operator and ask repetitive math questions with it?
I have a simple math problem. I am having problems using random imports. The idea is that there is a poll of 10 random questions. I have numbers from (0.12) using the random.randint function that works fine. Its the next bit of choosing a random operator I'm having trouble with ['+', '-', '*', '/'].
I have more complex coding at school, but this is my practice where all I need is the ability to randomly ask and ask a question, and also answer it myself to determine if the answer matches the correct one. Here's my code:
import random ops = ['+', '-', '*', '/'] num1 = random.randint(0,12) num2 = random.randint(0,10) operation = random.choice(ops) print(num1) print(num2) print(operation) maths = num1, operation, num2 print(maths)
As of now, my result is a little flawed. For example:
3
6
*
(3, '*', 6)
It is clear that he cannot determine the answer from (3, '*', 6). I will turn this operation into a subroutine in my other program, but it should work first!
And forgive me if it's not done very well, it was a quick recovery from a task I left behind at school, and I'm also pretty new to this with limited knowledge. Thanks in advance!
source to share
Python has a function called eval () that evaluates strings that contain mathematical expressions.
import random ops = ['+', '-', '*', '/'] num1 = random.randint(0,12) num2 = random.randint(0,10) operation = random.choice(ops) print(num1) print(num2) print(operation) maths = eval(str(num1) + operation + str(num2)) print(maths)
You need to convert your numbers to strings because the function expects something like the string "4 * 2", "3 + 1", etc. etc.
source to share
How about you create a dictionary that maps an operator symbol (like "+") to an operator (like operator.add
). Then select, format the string and perform the operation.
import random
import operator
Generating a random math expression
def randomCalc():
ops = {'+':operator.add,
'-':operator.sub,
'*':operator.mul,
'/':operator.truediv}
num1 = random.randint(0,12)
num2 = random.randint(1,10) # I don't sample 0 to protect against divide-by-zero
op = random.choice(list(ops.keys()))
answer = ops.get(op)(num1,num2)
print('What is {} {} {}?\n'.format(num1, op, num2))
return answer
User request
def askQuestion():
answer = randomCalc()
guess = float(input())
return guess == answer
Finally, take a multi-disciplinary quiz
def quiz():
print('Welcome. This is a 10 question math quiz\n')
score = 0
for i in range(10):
correct = askQuestion()
if correct:
score += 1
print('Correct!\n')
else:
print('Incorrect!\n')
return 'Your score was {}/10'.format(score)
Some tests
>>> quiz()
Welcome. This is a 10 question math quiz
What is 8 - 6?
2
Correct!
What is 10 + 6?
16
Correct!
What is 12 - 1?
11
Correct!
What is 9 + 4?
13
Correct!
What is 0 - 8?
-8
Correct!
What is 1 * 1?
5
Incorrect!
What is 5 * 8?
40
Correct!
What is 11 / 1?
11
Correct!
What is 1 / 4?
0.25
Correct!
What is 1 * 1?
1
Correct!
'Your score was 9/10'
source to share
Use a list for operators like operator = ['+', '', '-', '/'] then you can use Then you can use random selection in your list to call random operator (+, -, /) x = (random.choice (operator)) Finally you will need to convert your num1 and num2 to strings something like this eval (str (num1) + x + str (num2)) This should make your quiz completely random
source to share