-
Notifications
You must be signed in to change notification settings - Fork 566
Open
Description
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
Metadata
Metadata
Assignees
Labels
No labels