简体   繁体   English

如何从 python 中的字符串中提取数学方程式?

[英]How to extract Math Equation from a string in python?

I have a problem with math equation and string.我对数学方程式和字符串有疑问。 If a user gives a math equation in string format how do I get that out of string and calculate values?如果用户给出字符串格式的数学方程式,我如何从字符串中获取它并计算值?

Input: 
x = 1
y = 2
formula = "x+y"
Output:
formula = x+y
formula = 3
>>> x, y = 1, 2
>>> formula = "x+y"
>>> print('formula = ' + formula)
formula = x+y
>>> print('formula = ' + str(eval(formula)))
formula = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM