This is a simple command-line text-based calculator written in Python. The program prompts the user to enter mathematical expressions, evaluates them, and displays the results. The calculator continues to accept new expressions until the user types 'exit'
to quit.
- Supports basic mathematical operations: addition (
+
), subtraction (-
), multiplication (*
), and division (/
). - Handles division by zero with an appropriate error message.
- Allows users to input complex mathematical expressions and provides instant evaluation.
- Loops continuously until the user decides to exit the program.
The calculate()
function is responsible for evaluating the user input (mathematical expression). It uses Python's built-in eval()
function to compute the result. The function handles the following:
- Valid expressions: It returns the result of the evaluated expression.
- Division by zero: Catches
ZeroDivisionError
and returns an appropriate error message. - Invalid expressions: Catches any other exceptions and returns a generic error message (
"Error: Invalid expression"
).
This is the core of the program. It runs in a loop, prompting the user for input, passing the input to the calculate()
function, and displaying the result. The loop continues until the user enters 'exit'
.
- User Input: The program asks for a mathematical expression.
- Exit Command: If the user enters
'exit'
, the program terminates. - Expression Evaluation: The entered expression is passed to the
calculate()
function, which evaluates it and returns the result or an error message.
This block ensures that the main()
function is only executed when the script is run directly, not when it's imported as a module in another script.
- Clone or download the script.
- Make sure you have Python installed on your system.
- Run the script using Python.
- Enter mathematical expressions like
5 + 3
,10 * 4
, etc., and pressEnter
. - To exit the program, type
'exit'
and pressEnter
.
Here’s how the program will interact with the user:
- The program will prompt for a mathematical expression.
- After the expression is entered, the result will be displayed.
- If the user enters an invalid expression (e.g., dividing by zero), the program will display an error message.
- The loop will continue until the user types
'exit'
.