I'm trying to make a quiz using Python 3. The quiz randomly generates two separate numbers and operator. But when I try to get the user to input their answer, this shows up in the shell:
<class 'int'>
I'm not sure what I need to do. Even if I type in the correct answer, it always returns as incorrect.
import random
import operator
operation=[
(operator.add, "+"),
(operator.mul, "*"),
(operator.sub, "-")
]
num_of_q=10
score=0
name=input("What is your name? ")
class_name=input("Which class are you in? ")
print(name,", welcome to this maths test!")
for _ in range(num_of_q):
num1=random.randint(0,10)
num2=random.randint(1,10)
op,symbol=random.choice(operation)
print("What is",num1,symbol,num2,"?")
if input(int)==(num1,op,num2):
print("Correct")
score += 1
else:
print("Incorrect")
if num_of_q==10:
print(name,"you got",score,"/",num_of_q)
This line is incorrect:
if input(int)==(num1,op,num2):
You must convert the input to int
and apply op
to num1
and num2
:
if int(input()) == op(num1, num2):
You almost had it working. The reason for the error was you were telling the input
command to display an int
as a prompt, rather than converting the returned value into an int
.
Secondly your method for calculating the answer needed fixing as follows:
import random
import operator
operation=[
(operator.add, "+"),
(operator.mul, "*"),
(operator.sub, "-")
]
num_of_q = 10
score = 0
name=input("What is your name? ")
class_name=input("Which class are you in? ")
print(name,", welcome to this maths test!")
for _ in range(num_of_q):
num1=random.randint(0,10)
num2=random.randint(1,10)
op, symbol=random.choice(operation)
print("What is",num1,symbol,num2,"?")
if int(input()) == op(num1, num2):
print("Correct")
score += 1
else:
print("Incorrect")
print(name,"you got",score,"/",num_of_q)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.