python - 临时转换 - 输出问题
问题描述
我有 4 行必需的代码,我需要在适当的行中添加: 编写函数以将华氏温度转换为摄氏温度,反之亦然。
为什么我要发布我的问题:输出“none”的初始问题解决了现在得到错误的数字。
我相信我理解的事情正在发生:我正在尝试打印一个没有返回语句的函数。因此,当我在函数 celsius_to_fahrenheit(100)) 上使用 print 时,它“正确”运行,但由于需要 4 行,我必须运行 print(celsius_to_fahrenheit(100)) 行。
添加问题之前我使用过的资源: https ://beginnersbook.com/2019/05/python-program-to-convert-celsius-to-fahrenheit-and-vice-versa/
https://en.wikipedia.org/wiki/Fahrenheit
我得到了什么:37.77777777777778
我所期待的:celsius_to_fahrenheit(100) 212.0
所需代码:我删除了多余的间距
def fahrenheit_to_celsius(temp):
def celsius_to_fahrenheit(temp):
if __name__ == "__main__":
print(celsius_to_fahrenheit(100))
代码解析:
def fahrenheit_to_celsius(temp):
temp = (temp -32)* 5/9
return(temp)
def celsius_to_fahrenheit(temp):
temp = (temp *9/5)+32
return(temp)
if __name__ == "__main__":
print(celsius_to_fahrenheit(100))
解决方案
这就是你应该如何从 python 函数返回
def fahrenheit_to_celsius(temp):
temp = (temp *9/5)+32
return temp
def celsius_to_fahrenheit(temp):
temp = (temp -32)* 5/9
return temp
if __name__ == "__main__":
print(celsius_to_fahrenheit(100))
推荐阅读
- mysql - 如何优化mysql查询以获得更快的结果
- javascript - 使用 React Hook setSomeSetting 更改组件状态落后了一步
- entity-framework - 为什么我的 EF Core Group by query with external join 正在本地评估?
- c - 如何在C中计算文件中的行数?
- python - 如何按具有另一列值条件的子组过滤熊猫数据框
- node.js - 如何根据带有nodejs(gm)的预定义多边形缝合两个图像
- javascript - Selenium 等待元素变得可见
- prolog - 使用约束在 ECLiPSe Prolog 中修剪对称解决方案
- swift - 命名函数参数中断闭包赋值的类型推断
- html - 为什么@media (max-device-width: 360px) 会受到@media (max-device-width: 1024px) 的影响?