6.1. Exercises

Function Questions

def add_stars(name):
    name = "**" + name + "**"
    return

Coding questions

Instructions:
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.

Making a Function from Existing Code

Let’s practice converting existing code into a function so that it complies with the DRY principle.

Tasks:

  • Using the code provided, transform it into a function named uppercase_count
  • The function should take in one argument and return the number of uppercases in the string
  • Test it on the string I hope you've Been Learning ALOT!
Hint 1
  • Are you defining your function with def uppercase_count(string):?
  • Are you returning the object uppercase_num2?
Fully worked solution:


Making a Function

Let’s practice making a function that returns the BMI (Body Mass Index) given a person’s weight and height.

Tasks:

  • Define a function and give it the name BMI_calculator.
  • It should take 2 arguments which can be called height, and weight.
  • BMI can be calculated as:

404 image


  • Make sure the function returns the calculated BMI.
  • Once you have created your function, use it to calculate the BMI of a person with a height of 62 inches and a weight of 105 lbs.
  • Save this in an object name bmi_calc.
Hint 1
  • Are you defining your function with def bmi_calculator(height, weight):
  • Are you returning (weight/(height**2)) * 703?
  • To use it, are you running bmi_calculator(62, 105)?
Fully worked solution: