0% found this document useful (0 votes)
3 views

WEEK 1011 -Simple Calculator Program in Python

The document outlines the creation of a simple calculator program in Python that performs basic arithmetic operations such as addition, subtraction, multiplication, and division based on user input. It details the steps including prompting for input, defining functions for each operation, and using conditional statements to execute the chosen operation. The program is designed to be user-friendly, guiding users through their choices and displaying the results of their calculations.

Uploaded by

cahill.dokun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

WEEK 1011 -Simple Calculator Program in Python

The document outlines the creation of a simple calculator program in Python that performs basic arithmetic operations such as addition, subtraction, multiplication, and division based on user input. It details the steps including prompting for input, defining functions for each operation, and using conditional statements to execute the chosen operation. The program is designed to be user-friendly, guiding users through their choices and displaying the results of their calculations.

Uploaded by

cahill.dokun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PYTHON PROGRAMMING

Simple Calculator Program in Python


To help understand the concepts of python programming in depth, we will create a simple
calculator program in Python that can perform basic mathematical operations such as
addition, subtraction, multiplication, or division, all of which depend upon the input given
by the user.
The approach that we shall be following is straightforward to understand.
 Prompting Input from the User. That is, we shall be accepting input for two variables.
 Define and Add operators or functions such as add(), subtract(), multiply() and
divide() to estimate respective functions.
 To make it similar to calculator, apply conditional statements (if…elif…else branching)
to make it works as per User's choice.

Step 1: Prompting Input from the User, we shall accept input for two variables.

In this step, we shall take the user's input using Python's input() function. It is the same
as when we enter numbers in an accurate calculator to perform arithmetic operations. We
shall ask the user to input two variables using each variable's input() function.
Let's have the program perform the prompt for the two numbers:
Code:
number_1 = input('Please, Enter the first number: ')
number_2 = input('Please, Enter the second number: ')
Output:
Please, Enter the first number: 20
Please, Enter the second number: 10
Step2: Define and Add operators or functions such as add(), subtract(), multiply() and
divide() to estimate respective functions.
Code:
number_1 = int(input('Please, Enter the first number: '))
number_2 = int(input('Please, Enter the second number: '))
# arithmetic operation: Addition
print('{} + {} = '.format(number_1, number_2))
sum = int(number_1) + int(number_2)
print(“the sum is: ”, sum)

# arithmetic operation: Subtraction


print('{} - {} = '.format(number_1, number_2))
sub = int(number_1) - int(number_2)
print(“product of ” {} and {} is {}.format(number_1, number_2 product))
# arithmetic operation: Multiplication
print('{} * {} = '.format(number_1, number_2))
product(number_1 * number_2)
print(product

# arithematic operation: Division


print('{} / {} = '.format(number_1, number_2))
print(number_1 / number_2)
Output:
Please, Enter the first number: 10
Please, Enter the second number: 20
10 + 20 = 30
10 - 20 = -10
10 * 20 = 200
10 / 20 = 0.5
Above, we have described each of the four basic arithmetic operations in Python using the
format() function. format() functions fill the placeholder and make the output formatted.
The user's input was now computed for each of the arithmetic operations we defined.
Step3: To make it similar to a calculator, apply conditional statements (if…elif…else
branching) to make it work as per the User's choice
To make it the user's choice, we shall define each arithmetic operation as a function using
the def function in Python. We will again ask for the user's input for the mathematical
operations that they want to perform.
Code:
# Function to perform the arithmetic operation: Addition
def add(number_1, number_2):
return number_1 + number_2
# Function to perform the arithmetic operation: Subtraction
def subtract(number_1, number_2):
return number_1 - number_2
# Function to perform the arithmetic operation: Multiplication
def multiply(number_1, number_2):
return number_1 * number_2
# Function to perform the arithmetic operation: Division
def divide(number_1, number_2):
return number_1 / number_2
print("Hi, I am a Calculator!")
print("Please select which of the following arithematic operation you want me to
perform-\n" \
"1. Add\n" \
"2. Subtract\n" \
"3. Multiply\n" \
"4. Divide\n")
# Taking the input from the user for which arithmetic operation to perform
operation = int(input(" 1, 2, 3 or 4 :"))

number_1 = int(input('Please, Enter the first number: '))


number_2 = int(input('Please, Enter the second number: '))

if operation == 1:
print(number_1, "+", number_2, "=",
add(number_1, number_2))

elif operation == 2:
print(number_1, "-", number_2, "=",
subtract(number_1, number_2))

elif operation == 3:
print(number_1, "*", number_2, "=",
multiply(number_1, number_2))

elif operation == 4:
print(number_1, "/", number_2, "=",
divide(number_1, number_2))
else:
print("Please enter Valid input")
Output:
Hi, I am a Calculator!
Please select which of the following arithmetic operations you want to perform-
1. Add
2. Subtract
3. Multiply
4. Divide

1, 2, 3 or 4 :3
Please, Enter the first number: 10
Please, Enter the second number: 20
10 * 20 = 200

You might also like