我是 python 新手,已经开始学习函数。我在做家庭作业时遇到问题,无法创建将华氏温度转换为摄氏度的函数。请参阅下面我的代码。
def convert_f_to_c(temp_in_fahrenheit):
celsius = float(temp_in_fahrenheit - 32.00) * float(5.00 / 9.00)
return round(celsius)
convert_f_to_c()
参数是我们有一个代表温度的浮点,它返回一个代表摄氏度的温度的浮点,四舍五入到 1dp。
运行测试时我收到以下错误之一
TypeError: unsupported operand type(s) for -: 'str' and 'float'
我尝试创建一个将华氏温度转换为摄氏度的函数,但我不断收到错误。不确定如何继续回答这个问题。
最佳答案
看起来 temp_in_fahrenheit
是一个字符串,而它应该是一个 float 。只需更改 float 函数的调用方式即可。
#wrong
float(temp_in_fahrenheit - 32)
#better
float(temp_in_fahrenheit) - 32
关于python - 创建一个将华氏温度转换为摄氏度的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74425351/