1

我正在编写一个程序,它使用 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,这些值是在程序开始时定义的。

4

2 回答 2

1

您需要遍历温度和风速的所有值,每次都重新计算风寒。一个很好的方法可能是将 windChill 重新定义为一个函数。要使表格以正确的间距打印,您可以使用 字符串格式

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()

或者,首先将所有数据设置在二维数组(列表列表)中,然后使用单个长格式字符串将其打印出来。(这用于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))
于 2013-02-06T17:56:30.057 回答
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
于 2013-02-06T19:29:24.953 回答