2013-02-06 38 views
1

我正在编写一个打印带有windchill索引的表的程序。每个windchill值都应该足够用于相应的行和列。使用嵌套循环的windchill表

def main():  
    windSpeed = 0 
    temp = 0 
    windChill = 35.74 + (0.6215 * temp) - 35.75 * (windSpeed ** 0.16) \ 
      + 0.4275 * temp * (windSpeed ** 0.16) 

    # table frame, temps 
    for temp in range(-20, 70, 10): 
     print 2 * " ", temp, 
    print "\n", " " * 2, "-" * 51  

    #table frame, speeds 
    for windSpeed in range (0, 35, 5): 
     print windSpeed 


main() 

这将产生:

-20 -10 0 10 20 30 40 50 60 
    --------------------------------------------------- 
0 
5 
10 
15 
20 
25 
30 

显然,最困难的部分是实际打印Windchill的值。我一直在玩这个代码,它只打印出第一个windchill值,它的系数值为0和0,它们是在程序开始时定义的。

回答

1

因为它是不可能回去换线已经事后印刷,你需要计算风畏寒,同时您的打印列出表格的每一行。不要重复每一行中出现的温度和风速组合的冗长表达式,最好创建一个单独的函数来计算它们的值,然后重复调用(它的更短的)名称。

内置的字符串方法format()可以相对简单地显示所有正在按照所需方式计算的数据 - 因此花时间学习它的工作方式将是非常值得的尝试。

这是代码,这也是试图遵循 PEP 8 - Style Guide for Python Code

def wind_chill(temp, wind_speed): 
    """ Compute wind chill given temperature and wind speed if the 
     temperature is 50 degrees Fahrenheit or less and the wind speed is 
     above 3 mph, otherwise return 'nan' (not-a-number) because it's an 
     undefined quantity in those situations. 
    """ 
    return (35.74 + (0.6215 * temp) - 35.75 * (wind_speed ** 0.16) 
      + 0.4275 * temp * (wind_speed ** 0.16) 
       if temp <= 50 and wind_speed > 3 else 
      float('nan')) 

def main(): 
    # print table header 
    temps = xrange(-20, 70, 10) 
    num_temps = len(temps) 
    data = [" "] + [temp for temp in temps] 
    print ("{:3s}" + num_temps * " {:5d}").format(*data) 
    data = [" "] + num_temps * [5 * "-"] 
    print ("{:3s}" + num_temps * " {:5s}").format(*data) 

    # print table rows 
    row_format_string = "{:3d}" + num_temps * " {:5.1F}" 
    for wind_speed in xrange(0, 35, 5): 
     data = [wind_speed] + [wind_chill(temp, wind_speed) for temp in temps] 
     print row_format_string.format(*data) 

main() 

输出:

 -20 -10  0 10 20 30 40 50 60 
    ----- ----- ----- ----- ----- ----- ----- ----- ----- 
    0 NAN NAN NAN NAN NAN NAN NAN NAN NAN 
    5 -34.0 -22.3 -10.5 1.2 13.0 24.7 36.5 48.2 NAN 
10 -40.7 -28.3 -15.9 -3.5 8.9 21.2 33.6 46.0 NAN 
15 -45.0 -32.2 -19.4 -6.6 6.2 19.0 31.8 44.6 NAN 
20 -48.2 -35.1 -22.0 -8.9 4.2 17.4 30.5 43.6 NAN 
25 -50.8 -37.5 -24.1 -10.7 2.6 16.0 29.4 42.8 NAN 
30 -53.0 -39.4 -25.9 -12.3 1.3 14.9 28.5 42.0 NAN 
+0

你的职位是一个很有价值的给我,谢谢。 – nutship

+0

好听。我只是略微修改了'wind_chill()'函数,以便在我注意到某些计算的值似乎不合理并且在[Wikipedia]上查找它时会检查它的参数的有效性(http://en.wikipedia .ORG /维基/ Wind_chill#North_American_and_United_Kingdom_wind_chill_index)。 – martineau

1

您需要遍历temp和windspeed的所有值,每次重新计算wind chill。做一个很好的方法可能是重新定义windChill作为一个函数。要使用正确的间距打印表格,您可以使用string formatting

def main():  
    def wind_chill(temp, wind_speed): 
     return 35.74 + (0.6215 * temp) - 35.75 * (wind_speed ** 0.16) \ 
      + 0.4275 * temp * (wind_speed ** 0.16) 

    heading = ' ' 
    for temp in range(-20, 70, 10): 
     heading += "{:>7d}".format(temp) 
    print heading + "\n " + "-" * 62  
    for wind_speed in range (0, 35, 5): 
     output_line = "{:>2d}".format(wind_speed) 
     for temp in range(-20, 70, 10): 
      output_line += "{:>7.1f}".format(wind_chill(temp, wind_speed)) 
     print output_line 

main() 

替代地首先设置的所有数据中的2D阵列(列表的列表),然后使用单个长格式字符串打印出来。 (这里使用sum扁平化列表清单到一个单一的长序列。)

def main():  
    temps = range(-20, 70, 10) 
    winds = range(0, 35, 5) 
    chill = [[w] + [35.74 + .6215 * t - 35.75 * w**.16 + .4275 * t * w**.16 
        for t in temps] for w in winds] 
    rows = len(winds) 
    cols = len(temps) 
    print (' ' + '{:7d}' * cols + '\n ' + '-' * cols * 7 
      + ('\n{:2d}' + '{:7.1f}' * cols) * rows).format(*sum(chill, temps))