Python's method of realizing simple temperature conversion _python

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.