Basic Calculator
Goal
Build a simple Python program that can add, subtract, multiply, and divide two numbers.
Program Requirements
Ability to obtain/read two numeric inputs from the user of the program
Store/remember the two inputs
Do all the possible operations (add, subtract, multiply, divide) on the two numbers
Display the output
Pseudocode of the Calculator
Pseudocode 1: Basic Calculator
input NUMBER1
input NUMBER2
RESULT1 = NUMBER1 + NUMBER2
RESULT2 = NUMBER1 - NUMBER2
RESULT3 = NUMBER1 * NUMBER2
RESULT4 = NUMBER1 / NUMBER2
output RESULT1
output RESULT2
output RESULT3
output RESULT4
This program stores two inputs in two separate variables. Then it generates 4 more variables to store each of the resulting operation. Lastly, the program will output the stored operations to complete its execution.
A computer program is a set of instruction that a computer will follow to complete a given task; therefore, the computer will start from line 1 of the program, and it will execute all of the given instructions until the end of its instruction set.
Python Translation
# Basic Calculator
# Input Handling
number1 = int(input("Enter number 1: "))
number2 = int(input("Enter number 2: "))
# Operations
result1 = number1 + number2
result2 = number1 - number2
result3 = number1 * number2
result4 = number1 / number2
# Output
print(f"{number1} + {number2} = {result1}")
print(f"{number1} - {number2} = {result2}")
print(f"{number1} * {number2} = {result3}")
print(f"{number1} / {number2} = {result4}")
This Python program above is a direct translation of our program designed as a pseudocode.
The
#
symbol is used to create comments; a way to leave readable notes for the reader, but an ignored line of code by the Python Interpreter.input()
is a that reads a typed text within the .int()
is a built-in function that converts a given into an integer.print()
is a built-in function that outputs text-based data (called a ) to the console.
Within our print()
function, we are outputting formatted strings or "f-strings".
f-strings
are special types of strings that allow the direct values of variables embedded via curly braces {}
and it maintains the format of the strings based on how the data was defined.
Example Program Output
[INPUT]
Enter number1: 5
Enter number2: 8
[OUTPUT]
15 + 3 = 18
15 - 3 = 12
15 * 3 = 45
15 / 3 = 5
Connected Reading
Connected reading is a section dedicated to provide fundamental knowledge to the important aspects introduced in this page.
Last updated