We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b15ddc8 commit 60e44e3Copy full SHA for 60e44e3
calculator.py
@@ -0,0 +1,22 @@
1
+import re
2
+
3
+def is_safe_expression(expr):
4
+ # Allow only numbers, operators, and parentheses
5
+ return bool(re.match(r'^[\d\s+\-*/().]+$', expr))
6
7
+def calculate_bodmas(expression):
8
+ expression = expression.replace(' ', '') # remove spaces
9
+ if not is_safe_expression(expression):
10
+ return "Invalid input! Only numbers and basic operators are allowed."
11
+ try:
12
+ result = eval(expression)
13
+ return f"Result: {result}"
14
+ except Exception as e:
15
+ return f"Error: {e}"
16
17
+# Example usage
18
+while True:
19
+ user_input = input("Enter a BODMAS expression (or 'exit' to quit): ")
20
+ if user_input.lower() == 'exit':
21
+ break
22
+ print(calculate_bodmas(user_input))
0 commit comments