100 days of code. Day 2

This day was dealing with data types and specifically numbers.

We covered the int() and float() values, along with subscript printing. We also explored the type() function to identify if the value we are handing is a int, string or float.

BMI Calculator

There were several exercises and one of them is calculating the Body Mass Index (BMI) from the user’s height (in m) and weight (in kg). The formula goes to calculate is: weight / height^2

height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")

#forumla is weight / height^2
bmi = int(weight) / float(height)**2 

print (int(bmi))

Output:

$ python3 bmi.py
enter your height in m: 1.75
enter your weight in kg: 80
26

Tip Calculator:

We also got explored to F Strings, which saves us the trouble of converting values into a string without needing to converting the data types. I included this in a tip calculator exercise below:

The idea is simple: we take the total bill amount, add the tip and divide it among the number of people to pay.

For example, the bill is $123.45 and we want to tip 12% and split between two people: each person pays $69.13. (bill / # of people) * tip

print("Welcome to the tip calculator.")
bill = float(input("What was the total bill? $"))
tip = int(input("What percentage tip would you like to give? 10, 12, or 15? " ))
split = int(input("How many people to split the bill? "))
tipAsPercent = tip / 100
tipAmount = bill * tipAsPercent
billAndTip = bill + tipAmount

total = billAndTip / split
finalAmount = round(total,2)

print(f"Each person should pay: ${finalAmount}")

Output:

Welcome to the tip calculator.
What was the total bill? $123.45
What percentage tip would you like to give? 10, 12, or 15? 12
How many people to split the bill? 2
Each person should pay: $69.13