this is what you are looking for. Tip apreciate
lmk if it work
import random
def generate_formula(length):
operators = ['+', '-', 'x', '/']
formula = str(random.randint(1, 9))
for i in range(length-1):
operator = random.choice(operators)
number = str(random.randint(1, 9))
formula += operator + number
return formula
def calculate_formula(formula):
return eval(formula)
length = random.randint(6, 30)
formula = generate_formula(length)
answer = calculate_formula(formula)
print("What is the answer to this formula?")
print(formula)
user_answer = input("Your answer: ")
if int(user_answer) == answer:
print("Correct!")
else:
print("Incorrect. The answer is", answer)
This code generates a random formula with a length between 6 to 30, using the four basic arithmetic operators (+, -, x, /) and random numbers between 1 to 9. It then evaluates the formula using Python's eval() function, and prompts the user to input their answer. If the user's answer matches the calculated answer, it prints "Correct!", otherwise it prints "Incorrect."