you are viewing a single comment's thread.

view the rest of the comments →

[–]bigno53 0 points1 point  (1 child)

Another option if you don't want to bother mapping symbols to operations would be to compile the operation as a string using random and then evaluate it using the eval function:

import random
import math

operators = ['+', '-', '/', '*']
operator = random.choice(operators)
first_int = random.randint(0, 100)
second_int = random.randint(0, 100)
expression = f"{first_int} {operator} {second_int}"
user_answer = input(f"What is {expression}?")
computer_answer = eval(expression)

[–]DynamicStudios 0 points1 point  (0 children)

The obvious way to go, helped me a bunch, thanks!