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
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.")

# 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))
   print("Invalid input")

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
Welcome to the forum. What exactly is your question?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

(Oct-19-2018, 08:30 AM)buran Wrote: Welcome to the forum. What exactly is your question?

May guess would be
Quote:How to write ineffectife Python code
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.

Possibly Related Threads…
Thread Author Replies Views Last Post
  How To Create A "Birthday Calculator" in Python? unigueco9 3 219 Oct-11-2021, 08:03 PM
Last Post: SamHobbs
  python calculator only using decomposing functions kirt6405 1 386 Jun-19-2021, 12:52 AM
Last Post: bowlofred
  Suggestions for a simple data analysis program t4keheart 0 506 Mar-08-2021, 03:45 PM
Last Post: t4keheart
  How to make a Vocal synthesizer program on Python? Seadust 3 1,077 Jan-28-2021, 06:26 PM
Last Post: gonnach
  logging in simple program Inkanus 1 575 Dec-18-2020, 02:36 PM
Last Post: snippsat
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 742 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
  I try to make Heron formula program abcd 7 942 Oct-22-2020, 12:48 AM
Last Post: abcd
  Make a Python program executable in Windows Pedroski55 1 698 Sep-26-2020, 12:34 AM
Last Post: bowlofred
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 1,173 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  Iterate through all files in a folder and make simple calculation danielappleton 2 869 Apr-01-2020, 09:57 AM
Last Post: danielappleton

Forum Jump:

User Panel Messages

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