You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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)
print(math_ai("What is 5 + 3?"))
Output: 8
The text was updated successfully, but these errors were encountered: