-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDynamic Calculator
More file actions
26 lines (22 loc) · 960 Bytes
/
Dynamic Calculator
File metadata and controls
26 lines (22 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def dynamic_calculator():
print("Welcome to the Dynamic Calculator!")
print("You can perform operations like addition (+), subtraction (-), multiplication (*), division (/).")
print("Type 'exit' to quit the calculator.")
while True:
# Taking input from the user
user_input = input("\nEnter your mathematical expression: ")
# Check if the user wants to exit the calculator
if user_input.lower() == "exit":
print("Exiting the calculator. Goodbye!")
break
try:
# Evaluate the expression using Python's eval() function
result = eval(user_input)
print(f"Result: {result}")
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
except Exception as e:
print(f"Error: Invalid expression. {e}")
# Run the calculator
if __name__ == "__main__":
dynamic_calculator()