Python is a powerful programming language that makes it easy to create a variety of applications including a simple calculator. In this article, we will walk through the process of creating a basic calculator that can perform addition, subtraction, multiplication and division operations. This project is a great way for beginners to get familiar with Python syntax and basic programming concepts.
Step-by-Step Guide
1. Setting Up the Project
First, open your favorite text editor or IDE (Integrated Development Environment). Create a new Python file called calculator.py
.
2. Defining the Functions
We will define four basic functions for the calculator: addition, subtraction, multiplication and division.
def add(x, y): return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Error! Division by zero."
return x / y
Each function takes two arguments x
and y
and returns the result of the respective operation.
3. Creating the User Interface
Next, we will create a simple text-based user interface to allow users to select the operation they want to perform and input the numbers.
def calculator(): print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
while True:
choice = input("Enter choice (1/2/3/4): ")
if choice in ['1', '2', '3', '4']:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
else:
print("Invalid input. Please enter a number between 1 and 4.")
next_calculation = input("Do you want to perform another calculation? (yes/no): ")
if next_calculation.lower() != 'yes':
break
if __name__ == "__main__":
calculator()
Output :
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 1
Enter first number: 10
Enter second number: 20
10.0 + 20.0 = 30.0
Explanation
1. Function Definitions:
add(x, y)
: Returns the sum ofx
andy
.subtract(x, y)
: Returns the difference ofx
andy
.multiply(x, y)
: Returns the product ofx
andy
.divide(x, y)
: Returns the quotient ofx
andy
with a check to avoid division by zero.
2. User Interface:
- The
calculator()
function displays a menu for the user to select an operation. - It then takes the user input for the choice of operation and the numbers to be calculated.
- Based on the user’s choice it calls the appropriate function and displays the result.
- The program continues to prompt the user for further calculations until they choose to exit.
Running the Calculator
To run the calculator execute the calculator.py
file using Python.
python calculator.py
You should see a menu that allows you to select an operation and input numbers.
Conclusion
Creating a simple calculator in Python is an excellent way to practice basic programming concepts and get comfortable with functions, user input and control flow. This project can be expanded further to include more advanced features like handling more operations creating a graphical user interface (GUI) or even storing calculation history.
Post a Comment