Python 功能赢得';I don’我不想把它里面的格式看完
因此,我有一个代码,用于计算windchill在5到60之间的增量速度,用户输入温度,然后输入测量值,根据他们输入的内容,代码将调用华氏函数或摄氏函数。我遇到的问题是,每个函数中的格式似乎都不起作用。这是我目前的代码:Python 功能赢得';I don’我不想把它里面的格式看完,python,function,format,equation,Python,Function,Format,Equation,因此,我有一个代码,用于计算windchill在5到60之间的增量速度,用户输入温度,然后输入测量值,根据他们输入的内容,代码将调用华氏函数或摄氏函数。我遇到的问题是,每个函数中的格式似乎都不起作用。这是我目前的代码: import math t = float(input("What is the temperature?: ")) degree = input("Fahrenheit or celsius? Please put F or C: ")
import math
t = float(input("What is the temperature?: "))
degree = input("Fahrenheit or celsius? Please put F or C: ")
wciC = 13.12 + 0.6215*t - 11.37*math.pow(5, 0.16) + 0.3965*t*math.pow(5, 0.16)
wciF = 35.74 + 0.6215*t - 35.75*math.pow(5, 0.16) + 0.4275*t*math.pow(5, 0.16)
def fiveMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(5, 0.16) + 0.3965*t*math.pow(5, 0.16)
print("At temperature [0].0C, and wind speed 5 mph, the windchill is: [1]".format(t, wciC))
return fiveMileC
def fiveMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(5, 0.16) + 0.4275*t*math.pow(5, 0.16)
print("At temperature [0].0F, and wind speed 5 mph, the windchill is: [1]".format(t, wciF))
return fiveMileF
def tenMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(10, 0.16) + 0.3965*t*math.pow(10, 0.16)
return tenMileC
def tenMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(10, 0.16) + 0.4275*t*math.pow(10, 0.16)
print("At temperature [0].0F, and wind speed 10 mph, the windchill is: [1]".format(t, wciF))
return tenMileF
def fifteenMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(15, 0.16) + 0.3965*t*math.pow(15, 0.16)
return fifteenMileC
def fifteenMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(15, 0.16) + 0.4275*t*math.pow(15, 0.16)
print("At temperature [0].0F, and wind speed 15 mph, the windchill is: [1]".format(t, wciF))
return fifteenMileF
def twentyMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(20, 0.16) + 0.3965*t*math.pow(20, 0.16)
return twentyMileC
def twentyMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(20, 0.16) + 0.4275*t*math.pow(20, 0.16)
print("At temperature [0].0F, and wind speed 20 mph, the windchill is: [1]".format(t, wciF))
return twentyMileF
def twentyFiveMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(25, 0.16) + 0.3965*t*math.pow(25, 0.16)
return twentyFiveMileC
def twentyFiveMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(25, 0.16) + 0.4275*t*math.pow(25, 0.16)
print("At temperature [0].0F, and wind speed 25 mph, the windchill is: [1]".format(t, wciF))
return twentyFiveMileF
def thirtyMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(30, 0.16) + 0.3965*t*math.pow(30, 0.16)
return thirtyMileC
def thirtyMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(30, 0.16) + 0.4275*t*math.pow(30, 0.16)
print("At temperature [0].0F, and wind speed 30 mph, the windchill is: [1]".format(t, wciF))
return thirtyMileF
def thirtyFiveMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(35, 0.16) + 0.3965*t*math.pow(35, 0.16)
return thirtyFiveMileC
def thirtyFiveMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(35, 0.16) + 0.4275*t*math.pow(35, 0.16)
print("At temperature [0].0F, and wind speed 35 mph, the windchill is: [1]".format(t, wciF))
return thirtyFiveMileF
def fortyMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(40, 0.16) + 0.3965*t*math.pow(40, 0.16)
return fortyMileC
def fortyMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(40, 0.16) + 0.4275*t*math.pow(40, 0.16)
print("At temperature [0].0F, and wind speed 40 mph, the windchill is: [1]".format(t, wciF))
return fortyMileF
def fortyFiveMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(45, 0.16) + 0.3965*t*math.pow(45, 0.16)
return fortyFiveMileC
def fortyFiveMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(45, 0.16) + 0.4275*t*math.pow(45, 0.16)
print("At temperature [0].0F, and wind speed 45 mph, the windchill is: [1]".format(t, wciF))
return fortyFiveMileF
def fiftyMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(50, 0.16) + 0.3965*t*math.pow(50, 0.16)
return fiftyMileC
def fiftyMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(50, 0.16) + 0.4275*t*math.pow(50, 0.16)
print("At temperature [0].0F, and wind speed 50 mph, the windchill is: [1]".format(t, wciF))
return fiftyMileF
def fiftyFiveMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(55, 0.16) + 0.3965*t*math.pow(55, 0.16)
return fiftyFiveMileC
def fiftyFiveMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(55, 0.16) + 0.4275*t*math.pow(55, 0.16)
print("At temperature [0].0F, and wind speed 55 mph, the windchill is: [1]".format(t, wciF))
return fiftyMileF
def sixtyMileC(wciC, t):
wciC = 13.12 + 0.6215*t - 11.37*math.pow(60, 0.16) + 0.3965*t*math.pow(60, 0.16)
return sixtyMileC
def sixtyMileF(wciF, t):
wciF = 35.74 + 0.6215*t - 35.75*math.pow(60, 0.16) + 0.4275*t*math.pow(60, 0.16)
print("At temperature [0].0F, and wind speed 60 mph, the windchill is: [1]".format(t, wciF))
return fiftyMileF
if degree == "F":
fiveMileF(wciF, t)
tenMileF(wciF, t)
fifteenMileF(wciF, t)
twentyMileF(wciF, t)
twentyFiveMileF(wciF, t)
thirtyMileF(wciF, t)
thirtyFiveMileF(wciF, t)
fortyMileF(wciF, t)
fortyFiveMileF(wciF, t)
fiftyMileF(wciF, t)
fiftyFiveMileF(wciF, t)
sixtyMileF(wciF, t)
elif degree == "C":
fiveMileC(wciC, t)
tenMileC(wciC, t)
fifteenMileC(wciC, t)
twentyMileC(wciC, t)
twentyFiveMileC(wciC, t)
thirtyMileC(wciC, t)
thirtyFiveMileC(wciC, t)
fortyMileC(wciC, t)
fortyFiveMileC(wciC, t)
fiftyMileC(wciC, t)
fiftyFiveMileC(wciC, t)
sixtyMileC(wciC, t)
我希望它在每个单独的函数中运行方程,然后打印出来,但就我个人而言,如果有人能帮我的话,我无法找出代码的错误。这是因为你没有使用正确的符号。
对于string.format(),占位符应该位于花括号({和})内,而不是方括号([和])内。例如,在fiveMileC函数中,它应该是
print(“在温度{0}.0C,风速5 mph时,风寒是:{1}”。格式(t,wciC))
我建议只使用Ctrl-H来解决这个问题,这是一个非常简单的问题。您的函数不返回它们正在计算的实际值,而是尝试返回它们自己(这不起作用,因为它们尚未定义) 正如其中一条评论所指出的,您不需要复制并粘贴所有这些不同的函数。只需使用一个计算所有参数的函数,然后使用
范围
为每个所需风速调用它
from enum import Enum
class TempScale(Enum):
F = "F" # Fahrenheit
C = "C" # Celsius
def wind_chill(t: float, scale: TempScale, wind_speed: float) -> float:
return (
13.12
+ 0.6215 * t
- 11.37 * wind_speed ** 0.16
+ 0.3965 * t * wind_speed ** 0.16
) if scale == TempScale.C else (
35.74
+ 0.6215 * t
- 35.75 * 5 ** 0.16
+ 0.4275 * t * 5 ** 0.16
)
t = float(input("What is the temperature?: "))
degree = TempScale(input("Fahrenheit or Celsius? Please put F or C: "))
for speed in range(5, 61, 5):
print(
f"At temperature {t}{degree.value}, "
f"and wind speed {speed} mph, "
f"the windchill is: {wind_chill(t, degree, speed)}"
)
您的功能存在以下问题:
- 您的输入参数wciC和wciF不正确(在Python中不能像在通过引用传递参数的其他语言中那样以这种方式更新值)
- 您正在返回函数的名称(我不知道为什么)
import math
def calc_windchill(speed, t, deg):
' windchill based upon speed, temperature and deg (F or C) '
if deg == "F":
return 35.74 + 0.6215*t - 35.75*math.pow(speed, 0.16) + 0.4275*t*math.pow(speed, 0.16)
else:
13.12 + 0.6215*t - 11.37*math.pow(speed, 0.16) + 0.3965*t*math.pow(speed, 0.16)
使用-创建从5英里/小时到65英里/小时的结果
t = float(input("What is the temperature?: "))
degree = input("Fahrenheit or celsius? Please put F or C: ")
for speed in range(5, 65, 5):
print(f'Speed {speed}, Windchill {calc_windchill(speed, t, degree)}')
输入
What is the temperature?: 70
Fahrenheit or celsius? Please put F or C: F
Speed 5, Windchill 71.70916934582954
Speed 10, Windchill 70.82531333540499
Speed 15, Windchill 70.26098370128452
Speed 20, Windchill 69.83779238304979
Speed 25, Windchill 69.49585945951326
Speed 30, Windchill 69.20727414121399
Speed 35, Windchill 68.95662425691367
Speed 40, Windchill 68.73444792440651
Speed 45, Windchill 68.5344954040776
Speed 50, Windchill 68.3524106668557
Speed 55, Windchill 68.1850297182379
Speed 60, Windchill 68.02997800248346
输出
What is the temperature?: 70
Fahrenheit or celsius? Please put F or C: F
Speed 5, Windchill 71.70916934582954
Speed 10, Windchill 70.82531333540499
Speed 15, Windchill 70.26098370128452
Speed 20, Windchill 69.83779238304979
Speed 25, Windchill 69.49585945951326
Speed 30, Windchill 69.20727414121399
Speed 35, Windchill 68.95662425691367
Speed 40, Windchill 68.73444792440651
Speed 45, Windchill 68.5344954040776
Speed 50, Windchill 68.3524106668557
Speed 55, Windchill 68.1850297182379
Speed 60, Windchill 68.02997800248346
您返回的值不存在这是您需要24个函数的原因吗?看来这可以用一个来完成。还有,为什么要返回函数名而不是计算值?您建议我怎么做?很明显,我不想为自己做更多的工作,但这是我唯一能想到的方法,为了回应你的第二个评论,我不知道该怎么做