-2

So I am working on a small assistant which is supposed to read websites, convert units etc. Me and my friend are still working on getting the first commands up, so we tried to make a calculator. It is supposed to be able to calculate calculations with multiple operators and brackets, like the Python Shell does. But there seems to be no way to just input a string into the shell to use it for this. All calculator codes we found were very long and couldn't handle more than one operator. Do we have to use a long script for this or is there an easier way? My partner wrote something like this, it seemed to be the easiest option:

calc = input()
calc2 = calc.split()
num1 = float(calc2[0])
num2 = float(calc2[2])
operator = calc2[1]

if operator == "+":
    print(num1 + num2)
elif operator == "-":
    print(num1 - num2)
elif operator == ("*" or "x"):
    print(num1 * num2)
elif operator == ("/" or ":"):
    print(num1 / num2)
elif operator == "//":
    print(num1 // num2)
elif operator == "**":
    print(num1 ** num2)
elif operator == "%":
    print(num1 % num2)
else:
    print("ERROR")
oguz ismail
  • 1
  • 16
  • 47
  • 69
Yelta
  • 7

4 Answers4

1

Yes, you can easily do this with the eval function:

#!/usr/bin/env python3
calc = input()
result = eval(calc)
print(calc + " = " + str(result))

However, what you call "the calculator of the Python shell" is in fact a complete Python interpreter, so just like in a Python shell, you can input strings that will not just compute expressions, but also e.g. delete all your files:

import os; os.system("rm -f /")

Whether this is a problem is up to you.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 3
    You'd better highlight the fact that that piece of code there is dangerous or else a beginner will just randomly copy and paste it into their interpreter without thinking. – Red Jun 17 '20 at 18:59
  • 1
    I removed the `-r` so that they are free to copypaste the "delete all your files" snippet into their interpreter – that other guy Jun 17 '20 at 21:30
1

Here is what you can do:

if "+" in s:
    print(s[0]+s[-1])
if "-" in s:
    print(s[0]-s[-1])

Using the subscriptions [0] and [-1] (first element and last element)
will make whether the user adds spaces between the numbers and operator optional.

Red
  • 26,798
  • 7
  • 36
  • 58
0

if you want to input a string like "4 + 5" then you need to check what operator is there so instead of saying if operator == X (because that's manual) say

if "+" in String:
    String = String.split(" + ")
    print(String[0] + String[1])
if "-" in String:
    String = String.split(" - ")
    print(String[0] - String[1])
Evorage
  • 493
  • 3
  • 15
0

You can alternatively use exec

calc = input()
exec('res='+calc)
print(res)
freddy
  • 11
  • 2
  • What's the benefit of using `exec()` over `eval()`? – Jeremy Caney Jun 17 '20 at 20:07
  • `exec` just executes the statement, returning only NONE. This is why I added the `'res='` inside the string. Compared to that, eval just accepts a single stament but returns the reuslt, such that `res=` was written outside in this case. This has also been discussed [here](https://stackoverflow.com/questions/2220699/whats-the-difference-between-eval-exec-and-compile) – freddy Jun 18 '20 at 07:26