首页 > 解决方案 > 临时转换 - 输出问题

问题描述

我有 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))

标签: pythondata-conversiontemperature

解决方案


这就是你应该如何从 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))

推荐阅读