This article describes the Python method for achieving simple temperature conversion. Share to everyone for your reference. The specific analysis is as follows:
This is a simple Python code, the user converts different units of temperature, suitable for beginners to refer to
Copy Code code as follows:
def c2f (t):
Return (t*9/5.0) +32
def c2k (t):
Return t+273.15
def f2c (t):
Return (T-32) *5.0/9
def f2k (t):
Return (t+459.67) *5.0/9
def k2c (t):
Return t-273.15
def k2f (t):
Return (t*9/5.0)-459.67
Def get_user_input ():
User_input = 0
While type (user_input)!= Type (1.0):
User_input = Raw_input ("Enter degrees to convert:")
Try
User_input = float (user_input)
Except
Print User_input + "is not a valid entry"
Return User_input
def main ():
menu = "\ntemperature convertor\n\n" +\
"1 Celsius to fahrenheit\n" +\
"2 Celsius to kelvin\n" +\
"3 Fahrenheit to celsius\n" +\
"4 Fahrenheit to kelvin\n" +\
"5 Kelvin to Celsius\n" +\
"6 Kelvin to fahrenheit\n" +\
"7. Quit"
User_input = 0
While User_input!= 7:
Print Menu
User_input = raw_input ("Please enter a valid selection:")
Try
user_input = Int (user_input)
Except
Print User_input + "is not a valid selction, please try again\n"
If User_input = 1:
t = Get_user_input ()
Print str (t) + "degree Celsius is" + STR ((c2f (t)) + "degree Fahrenheit"
elif User_input = = 2:
t = Get_user_input ()
Print str (t) + "degree Celsius is" + STR ((c2k (t)) + "degree Kelvin"
elif User_input = = 3:
t = Get_user_input ()
Print str (t) + "degree Fahrenheit is" + STR ((F2C (t)) + "degree Celsius"
elif User_input = = 4:
t = Get_user_input ()
Print str (t) + "degree Fahrenheit is" + STR ((f2k (t)) + "degree Kelvin"
elif User_input = = 5:
t = Get_user_input ()
Print str (t) + "degree Kelvin is" + Str ((K2C (t)) + "degree Celsius"
elif User_input = = 6:
t = Get_user_input ()
Print str (t) + "degree Kelvin is" + Str ((K2F (t)) + "degree Fahrenheit"
elif User_input = = 7:
Quit ()
Else
Print str (user_input) + "is not a valid selection, please try again\n"
if __name__ = = "__main__":
Main ()
I hope this article will help you with your Python programming.