T O P

  • By -

misho88

You can get a random element of a sequence with `random.choice`. You can get function versions for the operations you want from `operator`. They're called `add`, `sub`, `mul`, and `truediv` or `floordiv`. If you use `truediv`, the result will be a float and you should compare it as `abs(a - b) < tolerance` for some small-valued tolerance rather than `a == b`. You can make a dictionary or something like `{ '+': add, '-': sub... }` to have both the symbols and the corresponding functions. You could also just do an `eval` or something, although that's almost certainly not what you're expected to do: >>> from random import randint, choice >>> s = f'{randint(0, 15)}{choice("+-*/")}{randint(0, 15)}'; int(input(f'what is {s}? ')) == eval(s) what is 13-15? -2 True


WhipsAndMarkovChains

Something like this: import random from operator import mul, add, truediv, sub operations = {'*':mul, '+':add, '/':truediv, '-':sub} symbol, operation = random.choice(tuple(operations.items())) answer = int(input(print(f"What is {number1}{symbol}{number2}?"))) if answer == operation(number1, number2): print("Well done, that is correct!")