Temperature Warning¶

Below is our original temperature converting program.

In [16]:
def temp_conv():
celsius = float(input("What is the celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print("The temperature is {0} degrees Fahrenheit.".format(fahrenheit))

In [17]:
temp_conv()

What is the celsius temperature? 20
The temperature is 68.0 degrees Fahrenheit.


Temperature conversion, with warnings¶

Now, let's add warnings for the cases of if the temperature in Fahrenheit is too hot (>= 90°F) or too cold (<= 30°F).

We use two simple conditions, where a condition is checked and only executed if the condition is evaluated to True.

The one dissadvantage of simple conditions is that redundant, mutually-exclusive conditions are all executed. It the fahrenheit is 190, it doesn't make sense to check if fahrenheit >= 90 and fahrenheit <= 30 (it's impossible to be both greater than 90° and less than 30° at the same time).

There is no harm in doing this in this simple program, but we will learn better techniques with two-way and multi-way decisions.

In [13]:
def temp_conv2():
celsius = float(input("What is the celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print("The temperature is {0} degrees Fahrenheit.".format(fahrenheit))

if fahrenheit >= 90:
print("It's really hot outside, get a lemonade.")

if fahrenheit <= 30:
print("It's really cold outside, get a hot chocolate.")

In [14]:
temp_conv2()

What is the celsius temperature? 80
The temperature is 176.0 degrees Fahrenheit.
It's really hot outside, get a lemonade.

In [15]:
temp_conv2()

What is the celsius temperature? -3
The temperature is 26.6 degrees Fahrenheit.
It's really cold outside, get a hot chocolate.