python - Automatic math equation generator not displaying correctly -
i trying write programme generates math problems. each problem should have next format:
<num> <op> <num> = ?
where each num represents random number between -50 , 50 , op randomly selected 4 basic math operators: +, -, /, *.
so far have:
import random operator import add, sub, mul, truediv def main(): random.seed() ops = (add, sub, mul, truediv) op = random.choice(ops) num1 = random.randint(-50,50) num2 = random.randint(-50,50) reply = op(num1, num2) reply = round(answer,2) print("welcome! here practice problem:\n") print(num1, op ,num2,"=?\n") main(
but when compile it, comes with, example:
-2 <built-in function truediv> -27 =?
instead of: -2 / -27 =?
how can displays operator instead of
<built-in function truediv>
make dict opsstr = {add :'+', sub: '-', ...}
, print corresponding entry selected operator.
so print like:
print(num1, opsstr [op] ,num2,"=?\n")
python function
No comments:
Post a Comment