1

I was working on a project in Python, which involves randomly selecting a operator (multiplication, division, addition, and subtraction), and then performing that operation on two integers. So far, my code will randomly select the operator in the form of a char. What I am struggling with is actually doing the math. Here is my code:

from random import randint
inputA = 2
inputB = 3
output = 0

desiredOutput = 5;
#possible operations: addition, subtraction, multiplication, division

add = "+"
multiply = "*"
divide = "/"
subtract = "-"

#choose randomely between the four

chooser = randint(1,4)
chosen = add
if chooser == 1:
    chosen = add
if chooser == 2:
    chosen = multiply
if chooser == 3:
    chosen = divide
if chooser == 4:
    chosen = subtract

Now what I want to do is take Input A and respectively multiply, divide, add or subtract it (using the "chosen" operator") from Input B.

Thanks, Avidh

Rao
  • 20,781
  • 11
  • 57
  • 77

4 Answers4

4

You could just perform the operation instead of storing the operators in a variable. Your ifs would then look like:

result = None
if chooser == 1:
    result = inputA + inputB
if chooser == 2:
    result = inputA * inputB
if chooser == 3:
    result = inputA / inputB
if chooser == 4:
    result = inputA - inputB
print(result)
gr1zzly be4r
  • 2,072
  • 1
  • 18
  • 33
3

A better way is to use functions instead of strings, and map the operator string to the function in a dictionary.

Module operator contains the functions that you need: add(), sub(), mul() and div(). You can set up a dictionary like this:

import operator

ops = {'+': operator.add,
       '-': operator.sub,
       '*': operator.mul,
       '/': operator.div} 

To randomly select an operator, make a random selection from the dictionary's keys:

import random

op = random.choice(ops.keys())
result = ops[op](inputA, inputB)
print('{} {} {} = {}'.format(inputA, op, inputB, result)

Alternatively you could use a list of tuples:

ops = [('+', operator.add), ('-', operator.sub), ...]
op, func = random.choice(ops)
result = func(inputA, inputB)
print('{} {} {} = {}'.format(inputA, op, inputB, result)
mhawke
  • 84,695
  • 9
  • 117
  • 138
2

Use a dictionary with lambda functions

import random

my_operators = {'+' : lambda a, b: a + b,
    '-': lambda a, b: a - b,
    '*': lambda a, b: a * b,
    '/': lambda a, b: a / b
}

actions = list(my_operators.keys())

chosen = random.choice(actions)
print (my_operators[chosen] (inputA, inputB))

Note that python has the operator module available which you can use to accomplish this:

import operator
my_operators = {'+' : operator.add,
    '-': operator.add,
    '*': operator.mul,
    '/': operator.div
}
smac89
  • 39,374
  • 15
  • 132
  • 179
0

(This looks like a homework problem, but here goes…)

One of the more straightforward and Pythonic ways to do this would be to use a functional programming style and pair each operation with an anonymous function (lambda) that executes that function:

from random import choice
inputA = 2
inputB = 3
output = 0

desiredOutput = 5;
#possible operations: addition, subtraction, multiplication, division

ops = dict(
    add = ("+", lambda a,b: a+b),
    multiply = ("*", lambda a,b: a*b),
    divide = ("/", lambda a,b: a/b),
    subtract = ("-", lambda a,b: a-b),
)

#choose randomly among the four operations

symbol, function = ops[ choice(ops.keys()) ]
output = function(inputA, inputB)
print ( "{inputA} {symbol} {inputB} = {output}".format(**locals()) )

Preemptive strike: Anyone who tells you to use exec or eval doesn't know Python, and if you don't understand why it's a bad idea then read this. (Actually there are reasons why they're dangerous for the real experts too.)

Dan Lenski
  • 76,929
  • 13
  • 76
  • 124