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 is a simplified, informal way of describing the steps and logic of a computer program or algorithm. It uses a mixture of natural language and basic programming structures to outline the flow of the program without being tied to the syntax of any specific programming language.
Pseudocode 1: Basic Calculator
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
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.
Why do we apply int()
function on our input()
function?
The input()
function will always read any data as a String-typed data. Therefore, we cannot treat the inputted value to invoke a numeric behavior (aka apply arithmetic operations). For the Python interpreter to properly understand how to use the given input, we must converted the inputs to integers for this situation.
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
Connected Reading
Connected reading is a section dedicated to provide fundamental knowledge to the important aspects introduced in this page.
Last updated