如何让用户在Python3中输入数字?

如何让用户在Python3中输入数字?,python,python-3.x,Python,Python 3.x,我正在尝试使用Python3做一个测验。测验随机生成两个独立的数字和运算符。但当我试图让用户输入答案时,shell中会显示: <class 'int'> 这行不正确: if input(int)==(num1,op,num2): 您必须将输入转换为int,并将op应用于num1和num2: if int(input()) == op(num1, num2): 你几乎让它工作了。错误的原因是您告诉input命令显示int作为提示,而不是将返回值转换为int 其次,计算答案的方法

我正在尝试使用Python3做一个测验。测验随机生成两个独立的数字和运算符。但当我试图让用户输入答案时,shell中会显示:

<class 'int'> 

这行不正确:

if input(int)==(num1,op,num2):
您必须将输入转换为
int
,并将
op
应用于
num1
num2

if int(input()) == op(num1, num2):

你几乎让它工作了。错误的原因是您告诉
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)

你想要
如果int(输入('输入数字:')==(num1,op,num2):
?@KevinGuan这应该是一个答案,这可能就是op想要的for@PreetKukreti谢谢,让我发布一个答案;)我尝试了这个方法,但结果仍然不正确。尝试@MichaelLaszlo的答案:)此外,
input(x)
使用
x
作为要打印的消息,其中
x
隐式转换为
str
int
本身就是
类型的名称。
type
的str通常是它的
repr
,对于int,它返回字符串
”。这表示您收到该消息的原因。@CyberSkull311请记住接受正确答案。
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)