简体   繁体   中英

Python3, best alternatives to eval()? (input as an expression)

I'm using Python 3.

I suppose the best way to ask this is how can I input an expression without using eval(input("Input: ")) ?

I'm a simple user right now, so what I needed eval for was an algebra calculator.

If you're the only person using that app and thus don't need to be worried about security issues, just keep using eval() or exec() .

Otherwise, just use a safe library for the specific task you need. Eg numexpr I guess for a calculator.

根据表达式的复杂程度, ast.literal_eval可能是更安全的替代方案。

how can I input an expression without using eval(input("Input: "))

simply don;t use eval . In Python 3.x, input is the replacement for raw_input so, you don;t need eval. You can simply write your statement as input("Input: ")

Moreover, in Python 2.X, you should have used raw_input("Input: ") instead of eval(input("Input: "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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