mathAI icon indicating copy to clipboard operation
mathAI copied to clipboard

Math ai

Open Ties2456 opened this issue 3 years ago • 0 comments

import re

def math_ai(question): # Use regular expressions to extract the numbers and operator from the question numbers = re.findall(r'\d+', question) operator = re.search(r'[+-*/]', question)

if not operator or not numbers:
    return "I'm sorry, I didn't understand the question."

# Convert the numbers to integers
numbers = [int(x) for x in numbers]

# Perform the calculation
if operator.group() == '+':
    result = sum(numbers)
elif operator.group() == '-':
    result = numbers[0] - sum(numbers[1:])
elif operator.group() == '*':
    result = 1
    for x in numbers:
        result *= x
elif operator.group() == '/':
    result = numbers[0]
    for x in numbers[1:]:
        result /= x

return result

print(math_ai("What is 5 + 3?"))

Output: 8

Ties2456 avatar Jan 14 '23 19:01 Ties2456