5

I am new to python and was trying to write something like this below (code A) so it does eactly like code B. I want to make use of user input of mathematical operators as do_what variable. How can we write this code (A) in python so it would work like code B?

code A

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
do_what = input("Enter a calculation symbol for calculation you want to perform: ")
result = float(num1) do_what float(num2)
print("result is: " + str(result))

code B

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
result = int(num1) + int(num2)
print("result is: " + str(result))
MAPK
  • 5,635
  • 4
  • 37
  • 88

4 Answers4

10

You can use the operator module for common operators and make a lookup dictionary to map symbols to functions. If you want operators not in that module, you can simply define custom functions and add look them up the same way:

import operator

operatorlookup = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul,
    '/': operator.truediv
}

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
do_what = input("Enter calculation symbols for calculation you want to perform: ")

op = operatorlookup.get(do_what)

if op is not None:
    result = op(float(num1), float(num2))
else:
    result = "Unknown operator"
print("result is: " + str(result))
Mark
  • 90,562
  • 7
  • 108
  • 148
6

You might also be interested in knowing about the inbuilt function eval. This reduces the if and else loops to a single statement for this particular example of yours

num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
do_what = input("Enter calculation symbols for calculation you want to perform: ")

result = eval(num1 + do_what + num2)
print("result is: %s" %result)

# Enter a number: 3
# Enter another number: 18
# Enter calculation symbols for calculation you want to perform: *
# result is: 54

EDIT

To make eval slightly safe in this particular case, you can perhaps use something like this

if do_what in ['+', '-', '*', '**', '/']:
    result = eval(num1 + do_what + num2)
    print("result is: %s" %result)
else:
    print ("Enter an arithmetic operator")
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • I have seen a bunch of times where `eval` is not recommended but it seems to be getting the job done. Are there downsides that one needs to worry about? – hqkhan Feb 06 '19 at 17:45
  • @hqkhan: Have a look at [this](https://stackoverflow.com/a/9384005/4932316) answer. As per one comment, it seems that as long as your `input` is not directly within `eval()`, you are fine – Sheldore Feb 06 '19 at 17:47
  • @hqkhan: Also, [this](https://stackoverflow.com/questions/35804961/python-eval-is-it-still-dangerous-if-i-disable-builtins-and-attribute-access) is worth reading – Sheldore Feb 06 '19 at 17:47
  • 1
    @hqkhan: Check my edit. That makes it slightly safer – Sheldore Feb 06 '19 at 17:51
2
num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
do_what = input("Enter calculation symbols for calculation you want to perform: ")
if do_what=='+':
    result = float(num1) + float(num2)
elif do_what=='-':
    result = float(num1) - float(num2)
elif do_what=='*':
    result = float(num1) * float(num2)
elif do_what=='/':
    result = float(num1) / float(num2)
print("result is: " + str(result))
Igor Dragushhak
  • 567
  • 1
  • 3
  • 14
0

If security is not your concern you could use eval to achieve this

result = eval(str(num1) + do_what + str(num2))

The problem is that eval literally evaluates everything you write as python code, so don't do this if you mean for this to be used by others

Crivella
  • 897
  • 6
  • 11