umerfarooq29 commited on
Commit
bf7e32c
·
verified ·
1 Parent(s): e20c13b

Create chatbot for calculations

Browse files
Files changed (1) hide show
  1. chatbot for calculations +36 -0
chatbot for calculations ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # calculator_chatbot.py
2
+
3
+ import re
4
+
5
+ def evaluate_expression(expression):
6
+ try:
7
+ # Evaluate the mathematical expression
8
+ result = eval(expression)
9
+ return result
10
+ except Exception as e:
11
+ return str(e)
12
+
13
+ def chatbot_response(user_input):
14
+ # Regular expression to find mathematical expressions
15
+ expression_pattern = r'(\d+\s*[\+\-\*\/]\s*\d+)'
16
+ match = re.search(expression_pattern, user_input)
17
+
18
+ if match:
19
+ expression = match.group(0)
20
+ result = evaluate_expression(expression)
21
+ return f"The result of {expression} is {result}."
22
+ else:
23
+ return "I'm sorry, I can only help with simple math calculations."
24
+
25
+ def main():
26
+ print("Welcome to the Calculator Chatbot! Type 'exit' to end the conversation.")
27
+ while True:
28
+ user_input = input("You: ")
29
+ if user_input.lower() == 'exit':
30
+ print("Chatbot: Goodbye!")
31
+ break
32
+ response = chatbot_response(user_input)
33
+ print(f"Chatbot: {response}")
34
+
35
+ if __name__ == "__main__":
36
+ main()