我尝试使用Python 3进行测验。测验随机生成两个单独的数字和运算符。但是当我试图让用户输入他们的答案时,这会显示在shell中:
<class 'int'>
我不确定自己需要做什么。即使我输入正确答案,它也总是返回错误。
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)
答案 0 :(得分:7)
此行不正确:
if input(int)==(num1,op,num2):
您必须将输入转换为int
并将op
应用于num1
和num2
:
if int(input()) == op(num1, num2):
答案 1 :(得分:1)
你差不多了。出现错误的原因是您告诉input
命令显示int
作为提示,而不是将返回的值转换为int
。
其次,您的计算答案的方法需要修复如下:
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)