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
Set Markers for Individual Points on a Line in Matplotlib
What Is the Recommended Way of Allocating Memory for a Typed Memory View
How to Execute a Python Script in Notepad++
Imports in _Init_.Py and 'Import As' Statement
How to Extract an Arbitrary Line of Values from a Numpy Array
Python Numpy/Scipy Curve Fitting
Calculation Error with Pow Operator
Multiple Levels of 'Collection.Defaultdict' in Python
Multithreaded Blas in Python/Numpy
How to Get Item's Position in a List
How to Keep Index When Using Pandas Merge
What Version of Visual Studio Is Python on My Computer Compiled With
Process to Convert Simple Python Script into Windows Executable
How Does the Key Argument in Python's Sorted Function Work
How to Terminate a Thread When Main Program Ends
List of Tables, Db Schema, Dump etc Using the Python SQLite3 API