使用嵌套循环的表,计算结果不正确/完全

发布于 2024-12-10 13:54:31 字数 3585 浏览 0 评论 0原文

这困扰了我几个小时,我已经接近结束了,我尝试了几种不同的变体,但结果却很奇怪。

我还发现这个问题被问过不同的时间,但他们似乎都没有遇到我似乎遇到的问题。

问题/问题: 我可以在这段代码下面的输出中看到。计算不会跨每一行的列进行,并且计算显然会在行和列中不断输入相同的值,并不断输出相同的值,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

This has stumped me for several hours, and I am so close to the end, and I've tried several different variations, and it comes out in weird ways.

I have also found different times this problem has been asked, but none of them seem to be having the problem I seem to be having.

The question / problem:
I am having can be seen in the output below this code. The calculation is not carrying across the columns for each respective row, and the calculation keeps taking in the same values into the row and col, apparently, and keeps outputting the same value, 35.7

The code:

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

This outputs:

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

Now, obviously, the 35.7 values are supposed to be different with each increment of 5 mph in wind speed, and it's supposed to calculate values all across the columns for each row, too.

The table and calculations are supposed to look similar to the table at:
http://www.nws.noaa.gov/os/windchill/index.shtml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-12-17 13:54:31

您在程序一开始就计算了 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 本身并没有神奇地改变 - 每次行/列更改时您都需要重新计算它。

You have calculated wchill at the very beginning of your program, when row = 0 and col = 0. That number is evaluated to 35.7 at the beginning, and will not change.

If you want wchill to change based on what row/column you are printing on currently, then you need to call the line

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

right before you print it, when the values of row and column change.

edit: what I am trying to say here is that wchill isn't magically changing by itself - you need to recalculate it every time the row/column changes.

你好,陌生人 2024-12-17 13:54:31

尽量减少非数据墨水!

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

将此打印结果设为您想要的实际值作为练习。

Minimize non-data ink!

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

Making this print the actual values you want is left as an exercise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文