Skip to content

Commit 60e44e3

Browse files
authored
Version (v1.0.0)
1 parent b15ddc8 commit 60e44e3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

calculator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)