We’ll discover how to create a basic calculator using Python. Feel free to utilize any online Python environment to write the code. I typically use this one: https://programiz.pro/ide/python.

Python’s straightforward syntax and clear structure make it perfect for developing a calculator application. This project will provide insight into essential concepts such as user input handling, conditional statements, and fundamental arithmetic operations.

The Code

num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))

if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
else:
result = "Invalid operator"

print("Result:", result)

Explanation

To understand this program. Let’s break it down into a few parts to understand it.

Part One: User Input

This section takes in the user input using the “input" function. The “float" function is used to convert the input into floating-point numbers, which allows for both integer and decimal inputs. The users will be asked to enter the first number, then an operator (+, -, *, /), and finally a second number.

Part Two: Conditional Statements

Lines 5 – 14 capture the conditional statements of the program. This block utilizes conditional statements to evaluate the entered operator and perform the corresponding arithmetic operation. If the operator is addition (+), the program adds the two numbers; if it’s subtraction (-), it subtracts the second number from the first, and so on. If the entered operator is not one of these, the program will set the result to say “Invalid Operator”.

  • if operator == "+":
    • If the entered operator is addition (+), the program executes the block of code indented under this “if” statement. It calculates the sum of num1 and num2 and assigns the result to the variable result.
  • elif operator == "-":
    • If the if statement above is not true (i.e., the operator is not +), the program checks this elif (else if) statement. If the operator is subtraction (-), it executes the corresponding block, subtracting num2 from num1 and assigning the result to result.
  • elif operator == "*":
    • Similarly, if the previous conditions are not met, this elif statement checks whether the operator is multiplication (*). If true, it calculates the product of num1 and num2 and assigns the result to result.
  • elif operator == "/":
    • This elif statement handles the case of division (/). If the operator is /, it performs the division of num1 by num2 and assigns the result to result.
  • else:
    • If none of the preceding if or elif conditions are true (i.e., the entered operator is not +, -, *, or /), the program executes the else block. In this case, it sets result to “Invalid operator,” indicating that the entered operator is not recognized.

Part Three: Output

The “print” statement displays the result of the calculation to the user. If a valid operator was entered, the result is shown; otherwise, a message will return saying “Invalid Operator”.

Demonstration

Here is a quick demonstration of the program when it is executed.

The program will first ask the user to enter a number

Next, an operator will be selected

After that, you will enter the second number

Finally, the result will be displayed

Leave a comment