Python program for arithmetic operations

Let’s create a Python program for arithmetic operations. Also known as a simple calculator in python.

In this case, we are going to implement only the four basic arithmetic operations: sum, subtraction, multiplication, and division. At the end of the post, I’ll give some hints on how to add more operations.

Python program for arithmetic operations menu example
Python program for arithmetic operations menu example

The menu for arithmetic operations

As we are going to implement four operations in the same program, we need some sort of mechanism to let the user choose what operation to execute.

For this program, we are going to create a menu for a python console application.

So, let’s start with the menu creation as suggested in the link above.

menu_options = {
    1: 'Add ',
    2: 'Subtract ',
    3: 'Multiply',
    4: 'Divide',
    5: 'Exit',
}

As we will have four options, we define the name of each option as in the code above.

Then, we create a function to display the four options (the menu).

Also, we need to create one function to handle this option. This step is optional. It is just a good practice to have our code more organized. If you don’t want to do it this way, you can always have all the code in your main program. Although I must tell you, it will make your code more difficult to maintain and debug.

def print_menu():
    for key in menu_options.keys():
        print (key, '--', menu_options[key] )

#add
def option1():
    print('Handle option \'Option 1\'')

#subtract
def option2():
    print('Handle option \'Option 2\'')

#multiply
def option3():
     print('Handle option \'Option 3\'')

#divide
def option4():
     print('Handle option \'Option 4\'')

if __name__=='__main__':
    while(True):
        print_menu()
        option = ''
        try:
            option = int(input('Enter your choice: '))
        except:
            print('Wrong input. Please enter a number ...')
        #Check what choice was entered and act accordingly
        if option == 1:
           option1()
        elif option == 2:
            option2()
        elif option == 3:
            option3()
        elif option == 4:
            option4()
        elif option == 5:    
            print('Thanks message before exiting')
            exit()
        else:
            print('Invalid option. Please enter a number between 1 and 5.')

After we finished with the menu, let’s implement how we are going to handle each option.

Because on each option we need two numbers to do the arithmetic operation, we should create a function that inputs the two numbers, so we separate the functionality of entering the two numbers from the code that carries on the operation.

The benefit is that if later on, we want to change the way we input the values, the code for the operations remains the same. This is called separation of concerns in programming, a good practice that is better to learn from the very beginning.

So, let’s write the code to input two numbers using the keyboard.

def enter_two_numbers():
    number1 = int(input('Enter the first number: '))
    number2 = int(input('Enter the second number: '))
    return number1, number2

As you can see, is very easy. But we can later change it to input these values from a file, or using voice, or any other input device.

I suggest you as a way to improve your programming skills, add exception handling to the code above. If you want to see what can go wrong with that code when you executed enter a letter instead of a number.

Now, let’s implement the functions that will handle each option. This will be simple functions because they are implementing simple operations.

#add
def option1():
    n1, n2 = enter_two_numbers()
    print('The sum is: ', n1+n2)

#subtract
def option2():
    n1, n2 = enter_two_numbers()
    print('The subtraction is: ', n1-n2)

#multiply
def option3():
     n1, n2 = enter_two_numbers()
     print('The multiplication is: ', n1*n2)

#divide
def option4():
     n1, n2 = enter_two_numbers()
     print('The division is: ', n1/n2)

That’s it!

You already have a python program for arithmetic operations or a simple calculator implemented in python.

H@ppy coding!

Related posts: