Today's approach: Python
Bonus: If you want to learn more about the origins of Python and why the language was created take a listen to this great podcast: https://www.redhat.com/en/command-line-heroes/season-3/pythons-tale
In this mini-project we are going to create a mini application in the terminal that converts the temperature from degrees kelvin to degrees fahrenheit. If you don't understand the difference between fahrenheit and kelvin that is ok. I'll share with you the formula as we walk through the steps.
I will list out the 6 steps below that you need to take to build out this mini-application. In the following video I will walk through all 6 steps myself so you can see a possible solution.
You can build out this application in any coding environment of your choice. I recommend using repl.it and choosing Python to create a free coding sandbox:
Challenge: Convert any given temperature in Kelvin to Fahrenheit
1. Let's imagine that the weather reports says that the temperature today will be 301 Kelvin. How should you dress for the day? Let's create an app that lets us know the temperature in fahrenheit. To start, create a variable named kelvin_temp, and set it equal to 301. Write a comment above that explains this line of code.
2. Finding the temperature in Celsius is similar to Kelvin — the only difference is that Celsius is 273.15 degrees less than Kelvin.
Let's convert Kelvin to Celsius by subtracting 273.15 from the kelvin_temp variable. Store the result in another variable, named celsius_temp.
Write a comment above that explains this line of code.
3. Use this equation to calculate Fahrenheit, then store the answer in a variable named fahrenheit_temp.
Fahrenheit = Celsius * (9/5) + 32
In the next step we will round the number saved to fahrenheit_temp. Write a comment above that explains this line of code.
4. Use the print() function to print the value of fahrenheit_temp. In our next step we are going to see what we can do to make sure that our number is a whole number by rounding down. The value you printed should begin with 82.13
5. Import the Math library. We are going to have to make use of its methods. At the top of our file add import math
As we have just seen, when you convert from Celsius to Fahrenheit, you often get a decimal number. Go ahead and remove the print command from step 4.
6. Use the print() function and string concatenation to log the temperature in fahrenheit_temp to the console to create the message as follows: The temperature is TEMPERATURE degrees Fahrenheit. TEMPERATURE should be determined by the value of fahrenheit_temp.
Great job! If you had any trouble with the code you can check out the solution below:
Step by Step Code Solution
Final Code:# Gain access to the `math` library to use `math.floor()` method import math # Temperature in kelvin stored in variable `kelvin_temp` kelvin_temp = 301 # temperature in celsius stored in variable `celsius_temp` celcisus_temp = kelvin_temp - 273.15 # convert celsius to fahrenheit stored in variable `fahrenheit_temp` fahrenheit_temp = celcisus_temp * ( 9 / 5 ) + 32 # round the value of fahrenheit down and assign to `fahrenheit` print(fahrenheit_temp) # round the value of fahrenheit down and assign to `fahrenheit_temp` fahrenheit_temp = math.floor(fahrenheit_temp) # use string concatenation to leave a message # "The temperature is `fahrenheit` degrees Fahrenheit." print("The temperature is", fahrenheit_temp, "degrees fahrenheit."