Python Forum
Python Program to Make a Simple Calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Program to Make a Simple Calculator
#1
In this example you will learn to create a simple calculator that can add, subtract, multiply or divide depending upon the input from the user.

To understand this example, you should have the knowledge of following Python programming topics:

Python Functions
Python Function Arguments
Python User-defined Functions

Source Code: Simple Caculator by Making Functions

# Program make a simple calculator that can add, subtract, multiply and divide using functions

# This function adds two numbers 
def add(x, y):
   return x + y

# This function subtracts two numbers 
def subtract(x, y):
   return x - y

# This function multiplies two numbers
def multiply(x, y):
   return x * y

# This function divides two numbers
def divide(x, y):
   return x / y

print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

# Take input from the user 
choice = input("Enter choice(1/2/3/4):")

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")
Output

Output:
Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): 3 Enter first number: 15 Enter second number: 14 15 * 14 = 210
In this program, we ask the user to choose the desired operation. Options 1, 2, 3 and 4 are valid. Two numbers are taken and an if...elif...else branching is used to execute a particular section. User-defined functions add(), subtract(), multiply() and divide() evaluate respective operations.

Good Luck Smile
Reply


Messages In This Thread
Python Program to Make a Simple Calculator - by jack_sparrow007 - Oct-19-2018, 07:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help trying to get my Python mortgage amortization calculator to work prope IamSirAskAlot 4 15,752 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  New to python, trying to make a basic calculator AthertonH 2 1,144 Apr-14-2022, 03:33 PM
Last Post: AthertonH
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,155 Feb-25-2022, 06:31 PM
Last Post: jao
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,807 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  How To Create A "Birthday Calculator" in Python? unigueco9 3 3,705 Oct-11-2021, 08:03 PM
Last Post: SamHobbs
  python calculator only using decomposing functions kirt6405 1 1,767 Jun-19-2021, 12:52 AM
Last Post: bowlofred
  Suggestions for a simple data analysis program t4keheart 0 1,789 Mar-08-2021, 03:45 PM
Last Post: t4keheart
  How to make a Vocal synthesizer program on Python? Seadust 3 3,566 Jan-28-2021, 06:26 PM
Last Post: gonnach
  logging in simple program Inkanus 1 1,717 Dec-18-2020, 02:36 PM
Last Post: snippsat
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 2,321 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020