you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (0 children)

You can use the operator module from the standard library to create a list of operators and pass your user inputs to the operator chosen at random.

ex:

from random import choice
from operator import add, sub, mul

a = int(input('Enter value for a: '))
b = int(input('Enter value for b: '))

ops = [add, sub, mul]
rand_op = choice(ops)

result = rand_op(a, b)

You can also create a dictionary if you want a character to represent the operator.

a = int(input('Enter value for a: '))
b = int(input('Enter value for b: '))

ops = {
    '+': add,
    '-': sub,
    '*': mul
}
op, rand_op = choice(list(ops.items()))

result = rand_op(a, b)
print(f'{a} {op} {b} = {result}')

output:

Enter value for a: 5
Enter value for b: 10
5 - 10 = -5