Making a Simple Python Calculator

Python Calculator


Making a Simple Python Calculator

Hello friends, today I will talk to you about how you can make a calculator with Python. While making the calculator, I will also show you basic things like decision structures in Python, getting input from the user, and creating functions. Happy reading.

First, let's plan out how we'll build the system in pseudo-code.

We will create 5 functions that will serve to perform our operations.
While 4 of our functions will perform mathematical operations, 1 function will inform the user, let them choose which operation to perform, and with which numbers. We will put the last function into an infinite loop so that the user does not have to close and reopen the program every time they want to perform an operation.
Now, let's see this plan in code.

Let's create our first functions for Addition, Subtraction, Division, and Multiplication operations.
In Python, functions are defined as "def functionname(a, b):".

# Addition Function
def Topla(x, y):
   return x + y
 
# Subtraction Function
def Cikar(x, y):
   return x - y
 
# Multiplication Function
def Carp(x, y):
   return x * y
 
# Division Function
def Bol(x, y):
   return x / y

Now let's create a function called "Islem" (Operation) and let this function display to the user which transaction they can choose with which number. Then, let's get two numbers and use decision blocks like if elif else to perform the operation with the numbers the user provided.

In Python, unlike other coding languages, "elif" is used instead of "else if". 

def Islem():
    # Displaying what the user can do
    print("Select the operation to perform. \n1 Addition \n2 Subtraction \n3 Multiplication\n4 Division")

    # Getting a choice from the user
    secim = input("Which operation do you want to perform? : ")

    # Getting numbers from the user
    sayi1 = int(input("Enter the 1st number: "))
    sayi2 = int(input("Enter the 2nd number: "))

    # Selecting the operation with an if loop.
    if secim == '1':
        print(sayi1,"+",sayi2,"=", Topla(sayi1,sayi2)) 
    elif secim == '2':
        print(sayi1,"-",sayi2,"=", Cikar(sayi1,sayi2)) 
    elif secim == '3':
        print(sayi1,"*",sayi2,"=", Carp(sayi1,sayi2)) 
    elif secim == '4':
        print(sayi1,"/",sayi2,"=", Bol(sayi1,sayi2))
    else:
        print("You entered an invalid operation number.")

Now let's write an infinite loop code and put this "Islem" function into the infinite loop, so that the user can perform new operations. 

# We call the operation function we prepared and put it in an infinite loop with while true to prevent the program from closing automatically
# and to prevent the user from having to reopen the program for every new operation.
while True:
    Islem()


Making a Simple Python Calculator

Now let's see all our code in one place.

# Addition Function
def Topla(x, y):
   return x + y
 
# Subtraction Function
def Cikar(x, y):
   return x - y
 
# Multiplication Function
def Carp(x, y):
   return x * y
 
# Division Function
def Bol(x, y):
   return x / y

def Islem():
    # Displaying what the user can do
    print("Select the operation to perform. \n1 Addition \n2 Subtraction \n3 Multiplication\n4 Division")

    # Getting a choice from the user
    secim = input("Which operation do you want to perform? : ")

    # Getting numbers from the user
    sayi1 = int(input("Enter the 1st number: "))
    sayi2 = int(input("Enter the 2nd number: "))

    # Selecting the operation with an if loop.
    if secim == '1':
        print(sayi1,"+",sayi2,"=", Topla(sayi1,sayi2)) 
    elif secim == '2':
        print(sayi1,"-",sayi2,"=", Cikar(sayi1,sayi2)) 
    elif secim == '3':
        print(sayi1,"*",sayi2,"=", Carp(sayi1,sayi2)) 
    elif secim == '4':
        print(sayi1,"/",sayi2,"=", Bol(sayi1,sayi2))
    else:
        print("You entered an invalid operation number.")

# We call the operation function we prepared and put it in an infinite loop with while true to prevent the program from closing automatically
# and to prevent the user from having to reopen the program for every new operation.
while True:
    Islem()