因此,我有一个代码,用于计算windchill在5到60之间的增量速度,用户输入温度,然后输入测量值,根据他们输入的内容,代码将调用华氏函数或摄氏函数。我遇到的问题是,每个函数中的格式似乎都不起作用。这是我目前的代码:
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)
我希望它在每个单独的函数中运行方程,然后打印出来,但就我的一生而言,我无法找出代码的错误,如果有人能帮助我,我将不胜感激
您的功能存在以下问题:
与24个函数不同,您可以按如下方式重新编码为单个函数
代码
从5英里/小时到65英里/小时的使用创建结果
输入
输出
这是因为您没有使用正确的符号。
对于string.format(),占位符应该位于花括号({和})内,而不是方括号([和])内。例如,在fiveMileC函数中,它应该是
print("At temperature {0}.0C, and wind speed 5 mph, the windchill is: {1}".format(t, wciC))
。我建议使用Ctrl-H来解决这个问题,这是一个非常简单的问题
您的函数不会返回它们正在计算的实际值,而是尝试返回它们自己(这不起作用,因为它们尚未定义)
正如其中一条评论所指出的,您不需要复制并粘贴所有这些不同的函数。只需要有一个计算所有参数的函数,然后使用
range
来调用每个所需的风速相关问题 更多 >
编程相关推荐