使用者输入温度数字,输入需要的转换类型,自定义函数将温度单位从华氏度转为摄氏度,或者相反;
module:temp
def temp_f_to_c(f): return (f - 32) * (5 / 9) def temp_c_to_f(c): return (c * 9 / 5) + 32 def main(): print(temp_c_to_f(100)) if __name__ == '__main__': main()main function:
import temps def convert_temp_system(temp, temp_system): if temp_system == 'c': new_temp = temps.temp_c_to_f(temp) else: new_temp = temps.temp_f_to_c(temp) return new_temp def print_temp_message(original_temp, new_temp, system): if system == 'f': print(original_temp, 'degrees F converted to C is ', new_temp) else: print(original_temp, 'degrees C converted to F is ', new_temp) def main(): temp = float(input('Enter the temperature: ')) system = input('F or C: ') converted_temp = convert_temp_system(temp, system) print_temp_message(temp, converted_temp, system) if __name__ == '__main__': main()
