Build a Simple Command-Line Calculator in Python
This tutorial will guide you through building a basic command-line calculator using Python. We’ll focus on understanding the core concepts and building a functional program step-by-step.
Example 1: Basic Addition
Let’s start with the simplest calculation – addition. We’ll create a program that takes two numbers as input and prints their sum.
# Get input from the user
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
# Convert the input strings to numbers (float allows decimals)
num1 = float(num1)
num2 = float(num2)
# Perform the addition
sum_result = num1 + num2
# Print the result
print("The sum is:", sum_result)
Explanation:
- We use the `input()` function to prompt the user to enter two numbers.
- `input()` always returns a string, so we need to convert these strings to numerical types using `float()`. Using `float()` makes our calculator capable of handling decimal numbers too.
- We perform the addition using the `+` operator.
- Finally, we use `print()` to display the result.
Common Mistake: Trying to directly add strings will concatenate them instead of performing mathematical addition. This is why the type conversion using `float()` is essential.
# Get input from the user
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
# Convert the input strings to numbers (float allows decimals)
num1 = float(num1)
num2 = float(num2)
# Perform the addition
sum_result = num1 + num2
# Print the result
print("The sum is:", sum_result)
Output:
Enter the first number: 10
Enter the second number: 5
The sum is: 15.0
Example 2: Implementing Subtraction, Multiplication, and Division
Now, let’s extend our calculator to support subtraction, multiplication, and division.
# Get input from the user
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
# Convert the input strings to numbers (float allows decimals)
num1 = float(num1)
num2 = float(num2)
# Perform the calculations
subtraction_result = num1 - num2
multiplication_result = num1 num2
division_result = num1 / num2
# Print the results
print("Subtraction result:", subtraction_result)
print("Multiplication result:", multiplication_result)
print("Division result:", division_result)
Explanation:
- We add code to perform the subtraction, multiplication, and division operations using the respective operators (`-`, “, `/`).
- We assign the results of these calculations to new variables.
- We use `print()` to display the results of each operation.
# Get input from the user
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
# Convert the input strings to numbers (float allows decimals)
num1 = float(num1)
num2 = float(num2)
# Perform the calculations
subtraction_result = num1 - num2
multiplication_result = num1 num2
division_result = num1 / num2
# Print the results
print("Subtraction result:", subtraction_result)
print("Multiplication result:", multiplication_result)
print("Division result:", division_result)
Output:
Enter the first number: 20
Enter the second number: 4
Subtraction result: 16.0
Multiplication result: 80.0
Division result: 5.0
Example 3: Adding Error Handling for Division by Zero
A crucial aspect of a robust calculator is handling potential errors. Specifically, we need to prevent division by zero, which would cause the program to crash.
# Get input from the user
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
# Convert the input strings to numbers (float allows decimals)
num1 = float(num1)
num2 = float(num2)
# Perform the calculations
if num2 == 0:
print("Error: Cannot divide by zero!")
else:
subtraction_result = num1 - num2
multiplication_result = num1 num2
division_result = num1 / num2
# Print the results
print("Subtraction result:", subtraction_result)
print("Multiplication result:", multiplication_result)
print("Division result:", division_result)
Explanation:
- We use an `if` statement to check if `num2` is equal to zero.
- If `num2` is zero, we print an error message.
- Otherwise, we proceed with the calculations as before.
# Get input from the user
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
# Convert the input strings to numbers (float allows decimals)
num1 = float(num1)
num2 = float(num2)
# Perform the calculations
if num2 == 0:
print("Error: Cannot divide by zero!")
else:
subtraction_result = num1 - num2
multiplication_result = num1 num2
division_result = num1 / num2
# Print the results
print("Subtraction result:", subtraction_result)
print("Multiplication result:", multiplication_result)
print("Division result:", division_result)
Output:
Enter the first number: 10
Enter the second number: 0
Error: Cannot divide by zero!
Output:
Enter the first number: 10
Enter the second number: 2
Subtraction result: 8.0
Multiplication result: 20.0
Division result: 5.0



Leave a Reply