7

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.

thefourtheye
  • 221,210
  • 51
  • 432
  • 478
Gavin
  • 133
  • 2
  • 2
  • 10

3 Answers3

9

Depending on how complicated your expressions are, ast.literal_eval may be a safer alternative.

Hugh Bothwell
  • 53,141
  • 7
  • 81
  • 98
6

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. E.g. numexpr I guess for a calculator.

millimoose
  • 37,888
  • 9
  • 76
  • 132
-3

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: "))

Feline
  • 575
  • 2
  • 14
Abhijit
  • 59,056
  • 16
  • 119
  • 195
  • 1
    But that won't address the OP's use case, which is "an algebra calculator". – DSM Feb 09 '14 at 15:55
  • 1
    You don't seem to understand, input() is only for strings, or I could add int() or float(), but I need to input expressions like (3 * x - 3), which seem to work only with eval. – Gavin Feb 10 '14 at 16:48