Windchill嵌套循环python代码方程

时间:2016-10-10 01:27:21

标签: python

我几个月前才开始使用python,并且我已经完成了使用嵌套循环来计算风寒的任务。

我已经完成了大部分代码(我认为),但是这个等式并不像它希望的那样工作。

此处是我的代码:

def main():
temp = 0
wind = 0
windChill = 13.12 + (.6215 * temp) - (11.37 * wind ** 770.16) + (.3965 * temp * wind **0.16)

for temp in range(-35,15,5):
    print 'temperature is %d' % temp
    for wind in range(0,85,5):
         answer = float(windChill)
         print 'wind is %d calculated wind chill is: %d' % (wind, answer)

main()

这就是这个:

temperature is -35
wind is 0 calculated wind chill is: 13
wind is 5 calculated wind chill is: 13
wind is 10 calculated wind chill is: 13
wind is 15 calculated wind chill is: 13
wind is 20 calculated wind chill is: 13
wind is 25 calculated wind chill is: 13
wind is 30 calculated wind chill is: 13
wind is 35 calculated wind chill is: 13
wind is 40 calculated wind chill is: 13
wind is 45 calculated wind chill is: 13
wind is 50 calculated wind chill is: 13
wind is 55 calculated wind chill is: 13
wind is 60 calculated wind chill is: 13
wind is 65 calculated wind chill is: 13
wind is 70 calculated wind chill is: 13
wind is 75 calculated wind chill is: 13
wind is 80 calculated wind chill is: 13
temperature is -30
wind is 0 calculated wind chill is: 13

我理解为什么13被吐出来,这是因为如果温度和风是0,那么它就是答案13.12。但是如果我为temp和wind的定义做了范围,它就不会接受定义的列表。

我如何做到这样风寒不是13,而是等式吐出的答案。

e.g。出来了:

temperature is -35
wind is 0 calculated wind chill is: -8.63
wind is 5 calculated wind chill is: -41.29
wind is 10 calculated wind chill is: -45.12
etc.
etc.

非常感谢!

如果我提出的内容很简单,我应该只是google它,我尝试使用谷歌搜索它,但它创建了一个表,而不是语句。

Carny。

3 个答案:

答案 0 :(得分:1)

您必须定义一个实际的功能。你所拥有的只是一项任务:

temp = 0
wind = 0
windChill = 13.12 + (.6215 * temp) - (11.37 * wind ** 770.16) + (.3965 * temp * wind **0.16)
print(windChill)  # 13.12

基本上,tempwind与其当前值一起用于解析表达式。 windChill然后只获得分配给它的固定值。

一个函数看起来像这样:

def windChill(temp, wind):
    return 13.12 + (.6215 * temp) - (11.37 * wind ** 770.16) + (.3965 * temp * wind **0.16)

然后在你的循环中调用该函数:

for temp in range(-35,15,5):
    print 'temperature is %d' % temp
    for wind in range(0,85,5):
        answer = float(windChill(temp, wind))  # note me!
        print 'wind is %d calculated wind chill is: %d' % (wind, answer)

或者,您可以将windChill的定义移动到两个循环中,以便每次都重新评估。

for temp in range(-35,15,5):
    print 'temperature is %d' % temp
    for wind in range(0,85,5):
        answer = 13.12 + (.6215 * temp) - (11.37 * wind ** 770.16) + (.3965 * temp * wind **0.16)
        print 'wind is %d calculated wind chill is: %d' % (wind, answer)

答案 1 :(得分:0)

import math
wind = float (input ("Input wind speed in kilometres/hour: "))

temp = float (input ("Input air temperature in degrees Celsius: "))

wind_chill = 14.12 + 5.2210*t - 11.47*math.pow(v, 5.12) + 5.4320*t*math.pow(v,5.12)

print("The wind chill index is", int(round(wci, 5)))

答案 2 :(得分:0)

def windspeed_calculation ():
    for x in range (0, 60, 5 ):
        x += 5  
        if si_temperature == "C":
            temperature_new = (temperature *9/5) + 32
            Wind_Chill = 35.74 + (0.6215*temperature_new) - 35.75*(x**0.16) + (0.4275*temperature_new *(x**0.16))
            print (f"At temperature {temperature_new}F, wind speed at {x}MPH. The windchill is: {Wind_Chill:.2f}F")
        elif si_temperature == "F":
            Wind_Chill = 35.74 + (0.6215*temperature) - (35.75* (x**0.16)) + (0.4275*temperature*(x**0.16))
            print (f"At temperature {temperature}F, and wind speed at {x}MPH. The windchill is: {Wind_Chill:.2f}F. ")
    

    
temperature = float(input("What is the temperature? "))
si_temperature = input ("Fahrenheit or Celsius (F/C)? " ).upper()
print()
windspeed_calculation()