[Python] python编写的简单温度转换程序 →→→→→进入此内容的聊天室

来自 , 2019-09-20, 写在 Python, 查看 140 次.
URL http://www.code666.cn/view/bc29e1f1
  1. 60
  2. 61
  3. 62
  4. 63
  5. 64
  6. 65
  7. 66
  8. 67
  9. 68
  10. 69
  11. 70
  12. 71
  13. 72
  14. 73
  15. 74
  16. 75
  17. 76
  18. def c2f(t):
  19.         return (t*9/5.0)+32
  20.  
  21. def c2k(t):
  22.         return t+273.15
  23.  
  24. def f2c(t):
  25.         return (t-32)*5.0/9
  26.  
  27. def f2k(t):
  28.         return (t+459.67)*5.0/9
  29.  
  30. def k2c(t):
  31.         return t-273.15
  32.  
  33. def k2f(t):
  34.         return (t*9/5.0)-459.67
  35.  
  36. def get_user_input():
  37.  
  38.         user_input = 0
  39.         while type(user_input) != type(1.0):
  40.                 user_input = raw_input("Enter degrees to convert: ")
  41.                 try:
  42.                         user_input = float(user_input)
  43.                 except:
  44.                         print user_input + " is not a valid entry"
  45.         return user_input
  46.  
  47. def main():
  48.  
  49.         menu = "\nTemperature Convertor\n\n"+\
  50.                 "1. Celsius to Fahrenheit\n"+\
  51.                 "2. Celsius to Kelvin\n"+\
  52.                 "3. Fahrenheit to Celsius\n"+\
  53.                 "4. Fahrenheit to Kelvin\n"+\
  54.                 "5. Kelvin to Celsius\n"+\
  55.                 "6. Kelvin to Fahrenheit\n"+\
  56.                 "7. Quit"
  57.                
  58.  
  59.         user_input = 0
  60.         while user_input != 7:
  61.                 print menu
  62.                 user_input = raw_input("Please enter a valid selection: ")
  63.  
  64.                 try:
  65.                         user_input = int(user_input)
  66.                 except:
  67.                         print user_input + " is not a valid selction, please try again\n"
  68.  
  69.                 if user_input == 1:
  70.                         t = get_user_input()
  71.                         print str(t) + " degree Celsius is " + str((c2f(t))) + " degree Fahrenheit"
  72.                 elif user_input == 2:
  73.                         t = get_user_input()
  74.                         print str(t) + " degree Celsius is " + str((c2k(t))) + " degree Kelvin"
  75.                 elif user_input == 3:
  76.                         t = get_user_input()
  77.                         print str(t) + " degree Fahrenheit is " + str((f2c(t))) + " degree Celsius"
  78.                 elif user_input == 4:
  79.                         t = get_user_input()
  80.                         print str(t) + " degree Fahrenheit is " + str((f2K(t))) + " degree Kelvin"
  81.                 elif user_input == 5:
  82.                         t = get_user_input()
  83.                         print str(t) + " degree Kelvin is " + str((k2c(t))) + " degree Celsius"
  84.                 elif user_input == 6:
  85.                         t = get_user_input()
  86.                         print str(t) + " degree Kelvin is " + str((k2f(t))) + " degree Fahrenheit"
  87.                 elif user_input == 7:
  88.                         quit()
  89.                 else:
  90.                         print str(user_input) + " is not a valid selection, please try again\n"
  91.        
  92. if __name__ == "__main__":
  93.         main()
  94. #//python/8979

回复 "python编写的简单温度转换程序"

这儿你可以回复上面这条便签

captcha