表使用嵌套循环,计算不正确/完全出现

时间:2011-10-17 22:10:44

标签: python loops tabular

这让我困扰了好几个小时,而且我已经接近尾声了,我尝试了几种不同的变化,并且它以奇怪的方式出现。

我也发现了这个问题的不同时期,但是他们似乎都没有遇到我似乎遇到的问题。

问题/问题: 我可以在此代码下面的输出中看到。对于每个相应的行,计算没有跨越列,并且计算显然在行和列中保持相同的值,并且保持输出相同的值,35.7

代码:

def WindChill():
row = 0
col = 0
i = 0
wchill = round((35.74 + 0.6215*(col) - 35.75*(row**16) + 0.4275*(col)*(row**16)), 1)

print(10 * " ", "|", end = "")

head = -1

for i in range(1):
    for col in range(-20, 70, 10):
        print(3 * " ", col, "F", 3 * " ", "|", end = " ")
    print("\n", 150 * "-")

while head < 0:
    for row in range(0, 55, 5):
        if (len(str(row))) < 2:
            print(row, "mph", 4 * " ", "|", end = " ")

        else:
            print(row, "mph", 3 * " ", "|", end = " ")

        print(3 * " ", round(wchill, 1), 3 * " ", "|", end = " ")

        col = 0
        head += 1

        print("\n", 150 * "-")
    print()

print()

输出:

                  |    -20 F     |     -10 F     |     0 F     |     10 F     |     20 F     |     30 F     |     40 F     |     50 F     |     60 F     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       0 mph      |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       5 mph      |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       10 mph     |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       15 mph     |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       20 mph     |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       25 mph     |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       30 mph     |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       35 mph     |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       40 mph     |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       45 mph     |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------
       50 mph     |     35.7     | 
       ------------------------------------------------------------------------------------------------------------------------------------------------------

现在,显然,35.7的值应该是不同的,风速每增加5英里/小时,它应该计算每行的所有列的值。

表格和计算看起来应该类似于以下表格: http://www.nws.noaa.gov/os/windchill/index.shtml

2 个答案:

答案 0 :(得分:2)

你在程序的最开始计算了wchill,当row = 0和col = 0.这个数字在开始时被评估为35.7,并且不会改变。

如果您希望wchill根据您当前打印的行/列进行更改,则需要调用该行

wchill = round((35.74 + 0.6215*(col) - 35.75*(row**16) + 0.4275*(col)*(row**16)), 1)

在打印之前,当行和列的值发生变化时。

编辑:我在这里要说的是,wchill本身并没有神奇地改变 - 你需要在每次行/列更改时重新计算它。

答案 1 :(得分:0)

尽量减少非数据墨水!

def WindChillTable(of):
    of.write('           -20\u00b0F')
    for col in range(-10, 70, 10):
        of.write('{: 6}  '.format(col))

    of.write('\n       \u250c' + 71*'\u2500')

    for row in range(0, 55, 5):
        if row == 0:
            of.write('\n mph 0 \u2502')
        else:
            of.write('\n    {:2} \u2502'.format(row))

        for col in range(-20, 70, 10):
            of.write('  {: 5.1f} '.format(0))

    of.write('\n')

if __name__ == '__main__':
    import sys
    WindChillTable(sys.stdout)

           -20°F   -10       0      10      20      30      40      50      60
       ┌───────────────────────────────────────────────────────────────────────
 mph 0 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
     5 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
    10 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
    15 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
    20 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
    25 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
    30 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
    35 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
    40 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
    45 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0
    50 │    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0

将此打印设为您想要的实际值,将其留作练习。