Projects: Calculator, BMI Calculator

Acadestine

Learning Objectives
    • Apply Arithmetic and Assignment operators to perform real-world calculations.
    • Use Operator Precedence to correctly translate mathematical formulas into Python code.
    • Combine Comparison and Logical operators to evaluate the results of your calculations.

Module Projects: Putting It All Together

Welcome to your first project lesson!

In the previous lessons, you collected some incredibly powerful tools: Arithmetic Operators (for doing math), Comparison Operators (for asking questions), Logical Operators (for combining rules), and Operator Precedence (for keeping traffic flowing smoothly).

Learning these concepts individually is great, but a true software engineer knows how to combine them to solve actual problems.

Today, we are going to build two practical projects using only the tools you have learned so far: a Basic Calculator and a Body Mass Index (BMI) Calculator. Let's get to work!

Project 1: The Basic Calculator

For our first project, we are going to build a script that takes two numbers and instantly calculates the results of all the primary math operations.

Imagine you are testing the backend logic for a new financial application. You need to verify that your addition, subtraction, multiplication, and division are all returning the correct values.

Since we haven't learned how to ask the user to type in numbers just yet, we will define our numbers directly in the code using variables.

Take a look at the sandbox below. We have set up the variables, and it is up to the computer to do the heavy lifting.

Console

        

Try changing first_number and second_number in the sandbox to any numbers you like, then run it again to see how quickly Python processes the math!

Project 2: The BMI Calculator

Now let's build something a little more complex. We are going to calculate a person's Body Mass Index (BMI).

Doctors and health professionals use BMI to quickly estimate if a person has a healthy body weight for their height. To build this, we need to know the official formula:

BMI = weight in kilograms divided by (height in meters squared)

This is where Operator Precedence becomes extremely important! We need to make sure the computer squares the height before it does the division. We can use parentheses () and the exponentiation operator ** to force Python to follow the rules of the math road.

Let's calculate the BMI for a user named Augustine, who weighs 75 kilograms and is 1.8 meters tall:

Console

        
Why did we get a super long decimal?

When you run the code above, you might see a result like 23.148148148148145. Division in Python always returns precise decimal numbers (called floats). Later in this course, you will learn how to round these numbers off so they look a bit cleaner for the end user!

Adding Logic: Are We In The Healthy Range?

Calculating the BMI is a great start, but software is supposed to give us meaningful answers. A user doesn't just want a number; they want to know, "Is my BMI considered healthy?"

According to standard medical guidelines, a BMI is generally considered "healthy" if it is greater than or equal to 18.5 AND less than or equal to 24.9.

We can use our Comparison Operators (>= and <=) along with our Logical Operator (and) to evaluate the BMI we just calculated and get a simple True or False answer.

Let's add this logic to our calculator:

Console

        

This is a perfect example of how combining operators creates smart, automated decision-making in your code!

Module Summary

Congratulations on completing your first coding projects! By combining the concepts from this module, you successfully built tools that perform real-world calculations and logical evaluations.

Here is a quick recap of how you used your new skills today:

  • Arithmetic Operators: Used to perform standard math (calculating the basic calculator results and dividing the BMI).
  • Assignment Operators: Used to update variables efficiently (like doubling our first number using *=).
  • Operator Precedence: Used parentheses () and the exponent ** to ensure the height was squared before division in our BMI formula.
  • Comparison & Logical Operators: Used >=, <=, and the and operator to evaluate if the final BMI fell within a specific numerical range.
Previous Next Lesson