Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What would I add to my code so that it returns "False" when one inputs a temperature that is below absolute zero (–273.15 °C; –459.67 °F)

my code:

def f2c(fahrenheit):
    #Converts F to C
    #USAGE: print(f2c(f_temp))
    celsius = (fahrenheit - 32) * 5/9
    return celsius

def c2f(celsius):
    #Converts C to F
    #USAGE: print(c2f(c_temp))
    fahrenheit = celsius * 9/5 + 32
    return fahrenheit


What I have tried:

I've yet to try anything. I will be importing this "helper" module into another module, where I can test and use it
Posted
Updated 12-Oct-21 11:00am
Comments
Member 15329613 12-Oct-21 16:54pm    
Wouldn't you need an if statement to test the value?

1 solution

The answer to your question is to use an if statement e.g.
Python
def f2c(fahrenheit):
   if(fahrenheit < 459.67):
       return False
   return (fahrenheit - 32) * 5/9
Consider whether that's really what you want to do, though. Maybe throwing an exception of some kind when an invalid temperature is entered might be a better idea.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900