Python 摄氏到华氏度
我在运行代码时收到一条错误消息 这是我的密码:Python 摄氏到华氏度,python,Python,我在运行代码时收到一条错误消息 这是我的密码: def celsius_to_fahrenheit(celsius): fahrenheit = celsius * 9/5 + 32 print(celsius_to_fahrenheit(-280)) if celsius < -273.15: return "That's impossible" else: fahrenheit = celsius * 9/5+32 return fahrenheit
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 9/5 + 32
print(celsius_to_fahrenheit(-280))
if celsius < -273.15:
return "That's impossible"
else:
fahrenheit = celsius * 9/5+32
return fahrenheit
print(celsius_to_fahrenheit(-273.4))
谢谢你的帮助 使用适当的缩进
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 9/5 + 32
if celsius < -273.15:
return "That's impossible"
else:
fahrenheit = celsius * 9/5+32
return fahrenheit
print(celsius_to_fahrenheit(-273.4))
我的回答更正了nishant改进压痕的回答 试试这个:
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 9/5 + 32
if celsius < -273.15:
return "That's impossible"
else:
fahrenheit = celsius * 9/5+32
return fahrenheit
print(celsius_to_fahrenheit(-273.4))
print(celsius_to_fahrenheit(-280))
让我知道它是如何工作的缩进在Python中很重要。由于您的第一个print语句具有与“def”相同的缩进,Python假定您的函数已经结束。因此,请确保函数中应该包含的所有内容都正确缩进 事实上,第一个print语句似乎不属于这里。将这种对函数的调用放在函数本身中会导致一切循环。此外: 没有必要检查其他的。如果绝对零检查通过,您仍将在该点退出该函数。 没有必要做两次计算。 至少在2.x版本中,Python在执行整数运算时不会自动切换到浮点。因此,以11为例,与某些版本的11.0相比,给出了不同的结果。但是,如果将9替换为9.0,或者仅将9/5替换为1.8,则可以强制Python使用浮动。 下面是一个简短的版本:
def celsius_to_fahrenheit(celsius):
if celsius < -273.15:
return "That's impossible"
return celsius * 9.0/5 + 32
print(celsius_to_fahrenheit(-50))
如前所述,您的函数只包含一行代码,其余代码将立即执行 正如错误消息所说,当您不在函数中时,无法返回 可能您的意思是大部分但不是全部代码都是函数的一部分。下面是一个带有注释的推测性重构
def celsius_to_fahrenheit(celsius):
# Don't perform this calculation twice
# fahrenheit = celsius * 9/5 + 32
# Don't attempt to call the function from within its own definition
if celsius < -273.15:
# raise an error instead of returning a bogus value
raise ValueError("Temperature below absolute zero")
else:
fahrenheit = celsius * 9/5+32
return fahrenheit
# We are now outside the function definition.
# Call the function twice to see that it works.
print(celsius_to_fahrenheit(-273.4))
# Should throw an error:
print(celsius_to_fahrenheit(-280))
要分解代码中的某些错误,请执行以下操作: 1:无返回语句 在这个块中,您需要包含一个返回函数 2:缩进不正确或未定义函数
对于这段代码,由于没有正确缩进,它不会作为函数调用的一部分执行。出现错误的原因是您试图返回函数中不存在的内容。不需要冗余代码。修复缩进,您可以使用:
def celsius_to_fahrenheit(celsius):
if celsius < -273.15:
return "That's impossible"
else:
return celsius * 9/5+32
print(celsius_to_fahrenheit(-273.4))
是的。如果没有缩进,则返回值不在函数内部。在Python2.7中,9/5是1。为了安全起见,最好将其改为9.0/5。你最好编辑并改进他的答案,而不是发布一个重复的答案。我在他编辑之前发布了答案,这就是为什么我似乎发布了一个重复的答案。我没有检查邮件上的时间戳。
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 9/5 + 32
print(celsius_to_fahrenheit(-280))
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 9/5 + 32
return fahrenheit
print(celsius_to_fahrenheit(-280))
if celsius < -273.15:
return "That's impossible"
else:
fahrenheit = celsius * 9/5+32
return fahrenheit
def celsius_to_fahrenheit(celsius):
if celsius < -273.15:
return "That's impossible"
else:
return celsius * 9/5+32
print(celsius_to_fahrenheit(-273.4))