python怎么编写温度程序

51次阅读
没有评论

共计 619 个字符,预计需要花费 2 分钟才能阅读完成。

你可以使用以下代码编写一个简单的温度转换程序:

def celsius_to_fahrenheit(celsius):
    fahrenheit = (celsius * 9/5) + 32
    return fahrenheit

def fahrenheit_to_celsius(fahrenheit):
    celsius = (fahrenheit - 32) * 5/9
    return celsius

# 输入摄氏温度
celsius = float(input(" 请输入摄氏温度:"))
# 调用函数将摄氏温度转换为华氏温度
fahrenheit = celsius_to_fahrenheit(celsius)
# 输出转换结果
print(" 华氏温度为:", fahrenheit)

# 输入华氏温度
fahrenheit = float(input(" 请输入华氏温度:"))
# 调用函数将华氏温度转换为摄氏温度
celsius = fahrenheit_to_celsius(fahrenheit)
# 输出转换结果
print(" 摄氏温度为:", celsius)

这个程序包含了两个函数,一个用于将摄氏温度转换为华氏温度,另一个用于将华氏温度转换为摄氏温度。在主程序中,首先输入摄氏温度,然后调用 celsius_to_fahrenheit() 函数将摄氏温度转换为华氏温度,并输出结果;然后输入华氏温度,调用 fahrenheit_to_celsius() 函数将华氏温度转换为摄氏温度,并输出结果。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计619字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)