loading...

Python Calculator CLI App

mqshaikh8 profile image Mohammed Shaikh ・4 min read

Before we start our calculator, It is always good practice to breakdown the steps that we would require to build it.

    -[]Welcome the user
    -[]Ask him to select an operation
    -[]Save that input
    -[]Ask him for the two numbers
    -[]Save those two numbers
    -[]make arithmetic operation functions
    -[]send the appropriate functions the number
    -[]Display the number back to the user 

First on that list would be to welcome the user. Let's print a nice welcoming message

print('Hello! User. Welcome to The Calculator')

When we run our program, it will print our welcoming message. The next step is to ask the user to select an operation.

print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")

That 2/8 of the deliverables done. The next step is for us to receive input and save it to a variable.

print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")
operation = input("Enter choice(1/2/3/4): ")

The reason we did not just ask the user to input the operation name directly rather than the number is to avoid edge cases. If we tell the user to input the operation name, the possible inputs could be "add" or "aDD" or "Add" or "ADD". The more input, the more chance of an accidental typo that can break our program. For simple applications, it is better to restrict inputs to the bare minimum so we do not spend a large time formatting the input.
The next step on the list is to ask for the two numbers that we are going to work with.

print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")
operation = input("Enter choice(1/2/3/4): ")
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")

We are asking for the numbers and saving it to two variables. One of the benefits of pseudo coding properly is that it becomes a good measurement of progress. Now we know we are more than halfway done because we have finished 5 out of the 8 steps

    -[x]Welcome the user
    -[x]Ask him to select an operation
    -[x]Save that input
    -[x]Ask him for the two numbers
    -[x]Save those two numbers
    -[]make arithmetic operation functions
    -[]send the appropriate functions the number
    -[]Display the number back to the user 

The next step is to make the code able to do the operations.

def add(x, y):
  print(f'{x} + {y} = {x+y}')
# This function subtracts two numbers 
def subtract(x, y):
  print(f'{x} - {y} = {x-y}')
# This function multiplies two numbers
def multiply(x, y):
  print(f'{x} * {y} = {x*y}')
# This function divides two numbers
def divide(x, y):
  print(f'{x} / {y} = {x+y}')
print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")
operation = input("Enter choice(1/2/3/4): ")
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")

We made 4 functions for each of the arithmetic operations. Whatever the user chooses we can send the numbers to that function and it will take care of it for us.The next step is to direct the numbers to the appropriate function.

def add(x, y):
  print(f'{x} + {y} = {x+y}')
# This function subtracts two numbers 
def subtract(x, y):
  print(f'{x} - {y} = {x-y}')
# This function multiplies two numbers
def multiply(x, y):
  print(f'{x} * {y} = {x*y}')
# This function divides two numbers
def divide(x, y):
  print(f'{x} / {y} = {x+y}')
print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")
operation = input("Enter choice(1/2/3/4): ")
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
if choice == '1':
   add(num1,num2)
elif choice == '2':
   subtract(num1,num2)
elif choice == '3':
   multiply(num1,num2)
elif choice == '4':
   divide(num1,num2)

The if statements would direct each of the 4 inputs to its appropriate function. If the user wants to add, they will input 1 and it would hit the first condition. The first condition would call and pass the number to the add function. The add function will display the added number in a formatted answer.

The above code will work , but it is messy. We need it to organize it better. Readability is very important for code. It is possible to add comments to organize and make it more readable, but good function and variable names and proper spacing does a better job.

def add(x, y):
  print(f'{x} + {y} = {x+y}')

def subtract(x, y):
  print(f'{x} - {y} = {x-y}')

def multiply(x, y):
  print(f'{x} * {y} = {x*y}')

def divide(x, y):
  print(f'{x} / {y} = {x+y}')

print('Hello! User. Welcome to The Calculator')
print("Pick an operation(1-Add)(2-Subtract)(3-Multiply)(4 - Divide)")

operation = input("Enter choice(1/2/3/4): ")
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")

if choice == '1':
   add(num1,num2)
elif choice == '2':
   subtract(num1,num2)
elif choice == '3':
   multiply(num1,num2)
elif choice == '4':
   divide(num1,num2)

That is the end of the tutorial. Hopefully everyone that reads it enjoys it as much as i enjoyed writing it.

Discussion

pic
Editor guide