Simple calculator in Python with source code

 Simple calculator in Python with source code



Source Code  :

 # creating while loop  
 while True:  
   # printing the available options  
   print("1. Addition")  
   print("2. Subtraction")  
   print("3. Multiplication")  
   print("4. Division")  
   print("5. Exit")  
   # asking user to Enter his choice  
   choice = int(input("Enter your choice: "))  
   # checking the choice between 1 and 4  
   if (choice>=1 and choice<=4):  
     # asking to enter two options  
     print("Enter two numbers: ")  
     # accepting first number  
     num1 = int(input())  
     # accepting second number  
     num2 = int(input())  
     # checking if number is 1  
     if choice == 1:  
       # adding  
       res = num1 + num2  
       # printing Addition  
       print("Result = ", res)  
     # checking if number is 2  
     elif choice == 2:  
       # Subtracting  
       res = num1 - num2  
       # printing Subtraction  
       print("Result = ", res)  
     # checking if number is 3  
     elif choice == 3:  
       # Multiplication  
       res = num1 * num2  
       # printing Result  
       print("Result = ", res)  
     # after checking all 3 choice,  
     # only one operation left, i.e. for Division  
     else:  
       # Division  
       res = num1 / num2  
       # printing Result  
       print("Result = ", res)  
   # checking if the choice is 5  
   elif choice == 5:  
     # if choice is 5, we will exit the program  
     exit()  
   # everything, except the five choices is useless  
   # so, we will print Wrong input..!!  
   else:  
     print("Wrong input..!!")  

Output :

 
 1. Addition  
 2. Subtraction  
 3. Multiplication  
 4. Division  
 5. Exit  
 Enter your choice: 1  
 Enter two numbers:   
 12  
 23  
 Result = 35  
 1. Addition  
 2. Subtraction  
 3. Multiplication  
 4. Division  
 5. Exit  
 Enter your choice: 2  
 Enter two numbers:   
 23  
 12  
 Result = 11  
 1. Addition  
 2. Subtraction  
 3. Multiplication  
 4. Division  
 5. Exit  
 Enter your choice: 3  
 Enter two numbers:   
 12  
 23  
 Result = 276  
 1. Addition  
 2. Subtraction  
 3. Multiplication  
 4. Division  
 5. Exit  
 Enter your choice: 4  
 Enter two numbers:  
 24  
 12  
 Result = 2.0  
 1. Addition  
 2. Subtraction  
 3. Multiplication  
 4. Division  
 5. Exit  
 Enter your choice: 5