函数不会遵循其中的格式
Function won't follow through with the format inside of it
所以我有一个代码设计用于以 5 到 60 之间的增量速度计算 windchill,用户输入温度,然后输入测量值,根据他们输入的内容,代码将调用华氏度函数或摄氏度函数.我遇到的问题是每个函数中的格式似乎不起作用。这是我目前的代码:
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("At temperature {0}.0C, and wind speed 5 mph, the windchill is: {1}".format(t, wciC))
.
我建议只使用 Ctrl-H 来解决这个问题,这是一个非常简单的问题。
您的函数不会 return 它们正在计算的实际值,它们会尝试 return 自己(这不起作用,因为它们尚未定义)。
正如其中一条评论所指出的,您不需要复制+粘贴所有这些不同的函数。只需要一个计算所有参数的方法,然后使用 range
为每个所需的风速调用它。
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 中以这种方式更新值,而在其他通过引用传递参数的语言中可以)
- 您正在返回函数的名称(我不确定为什么)
而不是 24 个函数,您可以重新编码为单个函数,如下所示。
代码
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 mph 的结果
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
所以我有一个代码设计用于以 5 到 60 之间的增量速度计算 windchill,用户输入温度,然后输入测量值,根据他们输入的内容,代码将调用华氏度函数或摄氏度函数.我遇到的问题是每个函数中的格式似乎不起作用。这是我目前的代码:
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("At temperature {0}.0C, and wind speed 5 mph, the windchill is: {1}".format(t, wciC))
.
我建议只使用 Ctrl-H 来解决这个问题,这是一个非常简单的问题。
您的函数不会 return 它们正在计算的实际值,它们会尝试 return 自己(这不起作用,因为它们尚未定义)。
正如其中一条评论所指出的,您不需要复制+粘贴所有这些不同的函数。只需要一个计算所有参数的方法,然后使用 range
为每个所需的风速调用它。
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 中以这种方式更新值,而在其他通过引用传递参数的语言中可以)
- 您正在返回函数的名称(我不确定为什么)
而不是 24 个函数,您可以重新编码为单个函数,如下所示。
代码
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 mph 的结果
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