Skip to content

Math ai #24

Description

@Ties2456

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions