You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何进一步精简计算风寒指数与温度转换的Python基础函数?

精简风寒指数计算与温度转换函数

我正在修一门大学基础编程课,期末项目要求写一个计算风寒指数的程序。这课太基础了,又没法跳过,我唯一的乐趣就是把简单任务复杂化,或者加些没必要的趣味功能。先说明:我已经完成项目了,不会提交任何别人写的代码。现在我想把现有的两个函数精简成一个不超过4行(含函数声明)的函数,项目要求必须包含风寒指数计算和温度转换功能(不用分开),还能调整传入的变量值。

现有代码片段:

def to_f(c):
    return c*9/5+32
def wind_chill(temp,wind):
    return 35.74 + 0.6215*temp - 35.75*wind**0.16 + 0.4275*temp*wind**0.16

我自己找到的一个解决方案:

def master(c,temp,wind):
    return str(35.74+0.6215*temp-35.75*wind**0.16+0.4275*temp*wind**0.16)+" "+str(c*9/5+32)

调用时通过split(" ")获取需要的返回值。

最终精简后的完整代码:

master=lambda c,temp,wind : str(35.74+0.6215*temp-35.75*wind**0.16+0.4275*temp*wind**0.16)+" "+str(c*9/5+32)
inp=input("What is the temperature? (Include F/C at end, example: 30F, 20C) ").replace(" ","")
if inp[len(inp)-1:].lower()=="c": inp=str(master(float(inp[0:len(inp)-1]),1,1).split(" ")[1])+inp[len(inp)-1:]
for i in range(5,65,5): print(f"At {round(float(inp[:len(inp)-1]),1)}F and a wind speed of {i} mph, the windchill is {round(float(master(0,float(inp[:len(inp)-1]),i).split(' ')[0]),2)}F.")

内容的提问来源于stack exchange,提问作者havebadtime

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 03:10:05