When fahrenheit is greater than 90 degree "hot" is to be return and when less than 30 degrees to return "cold", however the program is return 'None' as the return value. I understand that in python functions default to none as a return value, however I have state fahrenheit as my return value. Can someone please explain why this is occuring? I am a newbie to python and experimenting with chapter exercises to better understand the python language.
# convert2.py
# A program to convert Celsius temps to Fahrenheit
# This version issues heat and cold warning
def temp(fahrenheit):
if fahrenheit > 90:
fahrenheit = "hot"
return fahrenheit
if fahrenheit < 30:
fahrenheit = "cold"
return fahrenheit
def main():
celsius = float(input("What is the Celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print()
print("The temperature is", temp(fahrenheit), "degrees Fahrenheit")
main()
Think about the logic of your function. You're currently accounting for two cases:
But what if the temperature doesn't meet any of the cases above? What if the temperature isn't bigger than 90 degrees or lower than 30 degrees? See, your problem is that you're not accounting for a third possible case - the temperature is neither bigger than 90 degrees nor lower than 30, it's in between.
In your function, you need to decide on a value to return if the previous two conditions failed:
def temp(fahrenheit):
if fahrenheit > 90:
return "hot"
if fahrenheit < 30:
return "cold"
else: # if 30 < fahrenheit < 90 is True...
return 'error'