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

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.

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

[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