Eval function in python and finding four arithmetic

Enter a description:

Enter an expression formula

Output description:

Get the output

example

输入:3+2*{
    
    1+2*[-4/(8-6)+7]}
输出:25

Code:

Function 1: The eval() function can convert the string object into a valid expression to participate in the evaluation operation, and return the calculation result

while True:
    try:
        s = str(input())
        print(eval(s))
    except:
        break

Function 2: The eval() function removes the outermost quotation marks of the parameter and executes the rest of the statement

print(eval("'Hello world'")) # 输出 Hello world

#print(eval("Hello world"))  # 因为eval()函数去掉外部的双引号后,成为变量Hello world,而之前没有定义过该变量,所以会报错。

print(eval('"1+2"'))         # 输出1+2

Guess you like

Origin blog.csdn.net/weixin_43283397/article/details/108351997
Recommended