Python温度转换器round()函数无法按指定精度取整问题
温度转换器保留两位小数问题解决
我编写了一个用于自我测试的温度转换器项目,整体运行正常,但转换后的温度无法通过round()函数保留两位小数。已尝试在转换函数中为temp变量添加float()转换,但问题仍未解决。
代码如下:
scale_in = "" scale_out = "" temp = 0.0 converted_temp = 0.00 def toKelvin(): global temp global scale_in global scale_out global converted_temp scale_out = "Kelvin" if(scale_in == "c"): # Convert from Celsius to Kelvin converted_temp = float(temp) + 273.15 elif(scale_in == "f"): # Convert from Fahrenheit to Kelvin converted_temp = ((float(temp) - 32.0) / 1.799) + 273.15 return converted_temp def toCelsius(): global temp global scale_in global scale_out global converted_temp scale_out = "Celsius" if(scale_in == "f"): # Converts Fahrenheit to Celsius converted_temp = ((float(temp) - 32) * 5) / 9 elif(scale_in == "k"): # Convert Kelvin to Celsius converted_temp = float(temp) - 273.15 return converted_temp def toFahrenheit(): global temp global scale_in global scale_out global converted_temp scale_out = "Fahrenheit" if(scale_in == "c"): # Converts Celsius to Fahrenheit converted_temp = ((float(temp) * 1.8) + 32) elif(scale_in) == "k": # Kelvin to Fahrenheit converted_temp = (float(temp) * 1.8) - 459.67 return converted_temp def getTemp(): global temp # Get the current temperature from the user while(temp == 0.0): temp = input("What is the current temperature? ") # Check that we've been given a valid number try: float(temp) except ValueError: print("Invalid entry. Please enter a number.") temp = 0.0 return temp def get_scale_in(): scale = input('Converting from K, C or F?') return scale def get_scale_out(): scale = input("Converting to K, C or F?") return scale def convert(): global scale_in global scale_out global temp # Get the scale used by the user -- Kelvin, Celsius or Fahrenheit scale_in = get_scale_in() scale_out = get_scale_out() if(scale_out.lower() == "f"): converted_temp = round(float(toFahrenheit()), 2) elif(scale_out.lower() == "c"): converted_temp = round(float(toCelsius()), 2) elif(scale_out.lower() == "k"): converted_temp = round(float(toKelvin()), 2) else: print("Invalid input.") temp = getTemp() convert() # Print the result print(f"The temperature is {converted_temp} {scale_out}.")
示例输出:
What is the current temperature? 42 Converting from K, C or F?f Converting to K, C or F?c The temperature is 5.555555555555555 Celsius.
问题原因
在convert()函数中,直接给converted_temp赋值时,Python会默认创建一个局部变量,而非修改全局的converted_temp。最终打印的是全局变量的值,而全局变量在转换函数(如toCelsius())中被赋值的是未取整的原始计算结果,所以输出会显示长小数,round()的操作完全没作用到全局变量上。
解决方法
方法一:声明使用全局变量
在convert()函数开头添加global converted_temp,明确告诉Python要修改的是全局变量:
def convert(): global scale_in global scale_out global temp global converted_temp # 添加这行,声明使用全局变量 # Get the scale used by the user -- Kelvin, Celsius or Fahrenheit scale_in = get_scale_in() scale_out = get_scale_out() if(scale_out.lower() == "f"): converted_temp = round(float(toFahrenheit()), 2) elif(scale_out.lower() == "c"): converted_temp = round(float(toCelsius()), 2) elif(scale_out.lower() == "k"): converted_temp = round(float(toKelvin()), 2) else: print("Invalid input.")
方法二:避免全局变量(更推荐)
重构代码,让convert()返回转换后的温度值,主代码中接收该值并打印:
- 修改
convert()函数:
def convert(): global scale_in global scale_out global temp # Get the scale used by the user -- Kelvin, Celsius or Fahrenheit scale_in = get_scale_in() scale_out = get_scale_out() if(scale_out.lower() == "f"): return round(float(toFahrenheit()), 2) elif(scale_out.lower() == "c"): return round(float(toCelsius()), 2) elif(scale_out.lower() == "k"): return round(float(toKelvin()), 2) else: print("Invalid input.") return None
- 修改主代码部分:
temp = getTemp() converted_temp = convert() # Print the result if converted_temp is not None: print(f"The temperature is {converted_temp} {scale_out}.")
额外小问题修正
toFahrenheit()函数中有个语法冗余:elif(scale_in) == "k"多了一对括号,应该改为elif scale_in == "k",虽然不影响运行,但更符合Python代码规范。
内容的提问来源于stack exchange,提问作者Sabresong
相关产品推荐
相关产品推荐

