Case Study #1¶

Question 1¶

Given the equation for calculating Body Mass Index (BMI) is:

$$BMI = \frac{(\text{weight in pounds}) \times 703}{(\text{height in inches})^2}$$

Say an individual weighs 150 lbs and is 68 inches tall. What is their BMI?

weight = ____
height = ____
bmi = ____

Question 2¶

Given the area of a circle is:

$$A = \pi(\text{diameter}/2)^2$$

Say you have a 12" diameter pizza. Compute the area of the pizza, and assign it to the variable area.

area = ____

Hint: run from math import pi to use the variable pi for $\pi$.

Now, say that the cost of the pizza was $8. Compute the cost per square inch of this pizza.

cost_per_sqin = ____

Question 3¶

Given the variable language, which contains a string, use if/elif/else to write a function that:

  • returns "I love snakes!" if language is "Python" (with any kind of capitalization)
  • returns "Are you a pirate?" if language is "R" (with any kind of capitalization)
  • returns "What is language?" if language is any other value

Question 4¶

Say we have the following heights (in inches) and weights (in kg) for 10 children:

heights = [62, 58, 61, 61, 59, 64, 63, 61, 60, 62]
weights = [69, 62, 57, 59, 59, 64, 56, 66, 67, 66]

Using the BMI formula from earlier, and the conversion of 1 kg = 2.205 lbs, compute the BMI for each child. What is the average BMI?

average_bmi = ____

Question 5¶

Given this nested dictionary, extract the element containing "BANA":

d = {
    "a_list": [1, 2, 3],
    "a_dict": {
        "first": ["this", "is", "inception"],
        "second": [1, 2, 3, "BANA"]
    }
}

Question 6¶

Create a function divisible() that accepts two integers (a and b), and returns True if a is divisible by b without a remainder. For example, divisible(10, 3) should return False, while divisible(6, 3) should return True.

Once you have created this function, run the following code. Make sure to use the same seed values. What is your result?

In [ ]:
import random

random.seed(123)
a = random.randint(10, 100)
b = random.randint(1, 10)

divisible(a, b)

Question 7¶

Create a function lucky_sum that takes a list of integers, and returns their sum. However, if one of the values is 13, then it does not count towards the sum, nor do any values to its right.

For example, your function should behave as follows:

lucky_sum([1, 2, 3, 4])
10

lucky_sum([1, 13, 3, 4])
1

lucky_sum([1, 3, 13, 4])
4

lucky_sum([13])
0

Once you've created this function, run the following code. Be sure to run the code exactly as you see it. What is your result?

In [ ]:
import random

random.seed(18)
my_values = random.choices(range(1, 14), k=26)
lucky_sum(my_values)

Question 8¶

Run the following code. Read and embrace this mantra throughout this workshop, and your Python code-writing lives!

In [ ]:
import this