import re
def solve_math_problem(user_input):
user_input = user_input.lower().strip()
# Extract math expression
pattern = r"(\d+)\s*([+\-*/])\s*(\d+)"
match = re.search(pattern, user_input)
if not match:
return "Sorry, I can only solve basic problems like '2 + 2' or '10 / 5'."
num1 = int(match.group(1))
operator = match.group(2)
num2 = int(match.group(3))
try:
if operator == '+':
return f"The answer is {num1 + num2}"
elif operator == '-':
return f"The answer is {num1 - num2}"
elif operator == '*':
return f"The answer is {num1 * num2}"
elif operator == '/':
if num2 == 0:
return "Division by zero is undefined."
return f"The answer is {num1 / num2}"
except Exception as e:
return f"Error: {str(e)}"
def run_chatbot():
print("MathBot: Ask me math questions (e.g., 'What is 5 * 6?') or type 'bye' to quit.")
while True:
user_input = input("You: ")
if user_input.lower().strip() == "bye":
print("MathBot: Goodbye!")
break
response = solve_math_problem(user_input)
print("MathBot:", response)
if __name__ == "__main__":
run_chatbot()