-2

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
html92
  • 65
  • 9
  • The question / problem is: 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 – html92 Oct 17 '11 at 22:23
  • Your indentation as shown here is wacky, but it looks like you're doing the wchill calculation only once prior to entering the loops. Try moving your wchill calculation just before the `print( ... wchill ... )` line. – Russell Borogove Oct 17 '11 at 22:24
  • This isn't directly related to your problem, but you should learn to use [`format()`](http://docs.python.org/py3k/library/string.html#format-specification-mini-language). – zwol Oct 17 '11 at 22:51

2 Answers2

2

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.

Aurora
  • 4,384
  • 3
  • 34
  • 44
  • When I moved the wchill to after "for row in range(...)", it does output different numbers, but they're numbers like -3.5e+17, which is a tremendously large number. It also won't calculate and output values across all columns, still only in the first (-20F) column. – html92 Oct 17 '11 at 22:28
  • It's not going to be quite that simple. Remember that column is also changing, and the loop you put it inside is only iterating over 'row'. (Column will still be set to 0 in this case). When you print out wchill, you need to keep track of row AND col. Maybe think about two nested loops that go over rows and then columns. http://wiki.python.org/moin/ForLoop – Aurora Oct 17 '11 at 22:31
  • 1
    The giant numbers are because you're taking `row` to the 16th power instead of to the 0.16th power. – zwol Oct 17 '11 at 22:50
  • @Zack: Thank you for pointing that out. I now realize that, yes, the formula does say 0.16, not 16. I changed it (and several other things) and now it is working fine. – html92 Oct 17 '11 at 23:05
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)

&rightarrow;

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

zwol
  • 135,547
  • 38
  • 252
  • 361