How to Randomly Choose a Maths Operator and Ask Recurring Maths Questions with It

Random Arithmetic Quiz Problems

You need to call the quiz() function at the top level of your code:

quiz()

You're also missing:

import operator

Implement a random math operation (+, -, or *)

You can use the binary operator functions defined in the operator module. The functions operator.add(), operator.sub() and operator.mul() can all be called with two arguments to perform the operation their name suggests.

To select one of the three functions at random, you can simply put them in a list and then use random.choice():

operators = [operator.add, operator.sub, operator.mul]
random_operator = random.choice(operators)

The random operator can be applied to your numbers like this:

result = random_operator(number1, number2)

Python maths quiz random number

Use a for loop:

for num in range(5): 
# Replace "print" below, with the code you want to repeat.
print(num)

To repeat all questions, excluding "whats your name.." include the part of the code you need in the loop:

import random

name=input("What is your name?")
print ("Alright",name,"Welcome to your maths quiz")
score=0

for question_num in range(1, 11):
ops = ['+', '-', '*']
rand=random.randint(1,10)
rand2=random.randint(1,10)
operation = random.choice(ops)
maths = eval(str(rand) + operation + str(rand2))
print('\nQuestion number: {}'.format(question_num))
print ("The question is",rand,operation,rand2)

d=int(input ("What is your answer:"))
if d==maths:
print ("Correct")
score=score+1
else:
print ("Incorrect. The actual answer is",maths)

How could I change this Maths quiz in order to not use eval to find the answer in Python?

You could make a mapping from the symbol of the operator to an actual function that represents that operator:

import operator as op
operator_map = {"+":op.add, "-":op.sub, "*":op.mul}

then just change to

answer = operator_map[operator](n1, n2)

Python Maths Quiz- Outputting Correct or Incorrect

Your counter isn't incrementing within your while loop, as you have some serious indentation problems. As a result, your while loop just runs forever and you have an infinite loop. Make sure you indent the code including the counter which you wish to have in your while loop in order for that code to be executed and for your while loop to actually stop.

EDIT:
I fixed your indentation and your counter. Note that your division still will not work unless the quotient happens to be an integer, if you want to fix that you'll have to do some research yourself.

import random
import time
counter=0
score=0
count=0

function=['+','x','÷','-']

print('Welcome To The Arithmetic Quiz!')
name=input('Please enter you name.')
print('Thanks' , name , 'Lets Get Started!')

while counter <10:
firstnumber=random.randint(0,12)
secondnumber=random.randint(0,6)
operator=random.choice(function)

question=print(firstnumber, operator, secondnumber, '=')
userAnswer = input('Answer:')

if operator== '+':
count=firstnumber+secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif operator== 'x':
count=firstnumber*secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif operator== '-':
count=firstnumber-secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
elif operator== '÷':
count=firstnumber/secondnumber
if count == int (userAnswer):
print ('Correct!')
score= score+1
else:
print ('Incorrect')
counter += 1

print ("Your quiz is over!")


Related Topics



Leave a reply



Submit