[英]Maths Symbol From Variable
I have a challenge at school, but i am stuck with a small bit of my code. 我在学校遇到挑战,但是我的代码很少。
Here's what I have; 这就是我所拥有的; what I'm missing is how to finish the
answer =
line: 我所缺少的是如何完成
answer =
行:
while (questions <= 9):
randoms()
print("What is: ", num1, symbol, num2,"?:")
uanswer = int(input())
answer =
if uanswer == answer:
correct = correct + 1
else:
incorrect = incorrect + 1
questions = questions + 1
The variables num1
, num2
and symbol
are randomly generated, num1
and num2
are both integers, and symbol
is either '+'
, '-'
, '*'
or '/'
I was wondering how to use these to get an answer to an equation. 变量
num1
, num2
和symbol
是随机生成的, num1
和num2
都是整数,并且symbol
是'+'
, '-'
, '*'
或'/'
我想知道如何使用它们来获取对方程。
For example, num1
is 50
, symbol
is '-'
, and num2
is 25
. 例如,
num1
为50
, symbol
为'-'
, num2
为25
。 How would I use the variables to get the answer 25
? 我将如何使用变量获得答案
25
?
The clean way to do this is to create a mapping from symbols to functions. 干净的方法是创建从符号到函数的映射。
The only question is, what is the function behind the +
operator? 唯一的问题是,
+
运算符后面的功能是什么?
The answer is in the operator
module: a + b
is operator.add(a, b)
, and so on. 答案在
operator
模块中: a + b
是operator.add(a, b)
,依此类推。 So: 所以:
import operator
symbols = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.floordiv
}
I'm assuming here that you want 100 / 3
to be 33
, not 33.333333333333336
. 我假设在这里,你想
100 / 3
是33
,不是33.333333333333336
。 If I'm wrong, use truediv
instead of floordiv
above. 如果我错了,请使用
truediv
代替上述的floordiv
。 (See Numbers in the tutorial for an introduction to the difference, with links to more details.) (有关差异的介绍,请参见本教程中的数字 ,并提供更多详细信息的链接。)
Anyway, now, you can do this: 无论如何,现在,您可以执行以下操作:
answer = symbols[symbol](num1, num2)
Even if you didn't have operator
, you could always build a function for each operator: 即使您没有
operator
,也可以始终为每个运算符构建一个函数:
symbols = {
'+': (lambda a, b: a + b),
'-': (lambda a, b: a - b),
'*': (lambda a, b: a * b),
'/': (lambda a, b: a // b)
}
This may come in handy if you want to extend your language to a symbol that doesn't have a matching operator in Python. 如果您想将语言扩展到Python中没有匹配运算符的符号,这可能会派上用场。
If your maths language is a strict subset of Python's expression language (which it may not be—notice that the symbol '/'
maps to the Python operator //
if you want integers), and you're sure that you're never going to extend it in a way that isn't (eg, using ^
for exponentiation), and you're sure that you will never be using any numbers or symbols that you didn't generate yourself, it may be simpler to just build a Python expression and eval
it: 如果您的数学语言是Python表达语言的严格子集(可能不是,请注意-如果想要整数,请注意符号
'/'
映射到Python运算符//
),并且您确定永远不会以一种不扩展的方式(例如,使用^
进行幂运算)进行扩展,并且您可以确定自己永远不会使用未生成自己的任何数字或符号,只需构建一个Python表达式并eval
它:
expr = '{} {} {}'.format(num1, symbol, num2)
answer = eval(expr)
However, you should always be careful about using eval
; 但是,在使用
eval
时应始终小心; if the clean thing to do is simple enough for you to write, and to read and understand later, it's almost always a better answer. 如果要做的事情简单到足以使您编写,以后阅读和理解的程度,几乎总是一个更好的答案。
I think the best way is creating conditonals and check the symbol.. 我认为最好的方法是创建条件并检查符号。
if symbol=='-':
res = num1 - num2
elif symbol=='+':
res = num1 + num2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.