如何让python解决由变量组成的数学问题

如何让python解决由变量组成的数学问题,python,variables,math,random,Python,Variables,Math,Random,输入和输出错误示例: import random x = random.randrange(11) z = random.randrange(11) y = random.choice('-+*') print("what is?", x, y, z) ans1 = int(input()) if ans1 == (x, y, z): print("correct") score = score + 1 else: print("incorrect,the answ

输入和输出错误示例:

import random

x = random.randrange(11)
z = random.randrange(11)
y = random.choice('-+*')

print("what is?", x, y, z)
ans1 = int(input())

if ans1 == (x, y, z):
    print("correct")
    score = score + 1
else:
    print("incorrect,the answer is", x, y, z)
我如何使其打印?答案是12?我需要以某种方式计算表达式。

添加后

what is? 2 * 6

12

incorrect,the answer is 2 * 6
我得到:

print (x,y,z) + 3
回溯(最近一次呼叫最后一次):
文件“python”,第7行,在
TypeError:不支持+:“NoneType”和“int”的操作数类型
这意味着你的(x,y,z)永远不会等于任何给定的整数,因为它不是整数。

模块有,和

您可以将这些内容放入字典并查找:

>>> from operator import add, sub, mult
>>> add(5, 6)
11
>>> sub(5, 6)
-1
>>> mult(5, 6)
30

使用
eval
不是一个好方法。

你的问题在哪里?我如何回答一个由变量-x,y,z组成的数学问题?我的代码也有问题。你能纠正它吗?不确定是使用“随机”标记还是双关语?
操作符也包括
floordiv
truediv
,您还应该包括一个用于
/
的运算符。@MattDMo好吧,这个问题只有这三个运算符。我猜老师在避免被零除的错误。啊,我没注意到。
>>> from operator import add, sub, mult
>>> add(5, 6)
11
>>> sub(5, 6)
-1
>>> mult(5, 6)
30
from operator import add, sub, mult
ops = {'+': add,
       '-': sub,
       '*': mult}

ans = ops[y](x, z)