Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math ai #24

Open
Ties2456 opened this issue Jan 14, 2023 · 0 comments
Open

Math ai #24

Ties2456 opened this issue Jan 14, 2023 · 0 comments

Comments

@Ties2456
Copy link

Ties2456 commented Jan 14, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant