-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_based_calculator.py
More file actions
31 lines (25 loc) · 942 Bytes
/
text_based_calculator.py
File metadata and controls
31 lines (25 loc) · 942 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
27
28
29
30
31
# Text-Based Calculator
def calculate(expression):
try:
# Evaluate the mathematical expression
result = eval(expression)
return result
except ZeroDivisionError:
return "Error: Division by zero is not allowed."
except Exception:
return "Error: Invalid expression."
def main():
print("Text-Based Calculator")
print("Enter a mathematical expression (e.g. 3 + 3) or type 'exit' to quit.")
while True:
# Prompt the user to enter a mathematical expression
expression = input("Enter expression: ")
# If the user types 'exit', the program terminates
if expression.lower() == 'exit':
print("Exiting the calculator. Goodbye!")
break
# Call the calculate function and display the result
result = calculate(expression)
print(f"Result: {result}")
if __name__ == "__main__":
main()