THIS WINDCHILL TABLE PROGRAM IS RUNNING, BUT THE NUMBERS IS STILL NOT LINING UP STRAIGHT..WHY, CAN YOU PLEASE HELP ME..THANKS


HERE IS THE PROGRAM BEFORE I RUN IT..

def windchill( vel, temp):
   wc = 35.74 + .6215*temp - 35.75*(vel**.16) + .4275*temp*(vel**.16)
   return wc



def main():
    print (" __ __ __               Temperature \n")
    print ("Speed \n")
    print (" 1 -20 70 10 0 10 20 30 40 50 60 ")
    



    for x in range(5, 55, 5):
        print x
        for y in range (60, -30, -10):
            chill = windchill(x,y)
            print str(int(chill)) + " ",
       



main()

AND HERE IS THE RESULTS AFTER I RUN IT...SEE THE NUMBERS IS NOT LINE UP STRAIGHT..

__ __ __               Temperature 

Speed 

 1 -20 70 10 0 10 20 30 40 50 60 
5
59  48  36  24  12  1  -10  -22  -34  10
58  46  33  21  8  -3  -15  -28  -40  15
57  44  31  19  6  -6  -19  -32  -45  20
56  43  30  17  4  -8  -21  -35  -48  25
56  42  29  16  2  -10  -24  -37  -50  30
55  42  28  14  1  -12  -25  -39  -53  35
55  41  27  13  0  -13  -27  -41  -54  40
54  40  26  13  0  -14  -28  -42  -56  45
54  40  26  12  -1  -15  -29  -44  -58  50
54  39  25  11  -2  -16  -31  -45  -59

:evil: :mad: :o :cry: :(

Dani AI

Generated

The misalignment comes from two things: numbers have different widths (e.g., -10 vs 1), and the trailing comma in your inner print keeps the cursor on the same line, so the next print x shows up at the end of the previous row. is right about using a fixed width. Also make sure you end each row with a newline after the inner loop.

Here is a Python 2-friendly pattern that keeps everything in neat columns and prints the wind speed on the same line as its row. It right-justifies every cell to the same width and uses rounding (not truncation) so the values look cleaner.

def main():
    col = 5  # column width; bump to 6 if your numbers get wider

    # header
    print "".rjust(col),  # corner
    for t in range(60, -30, -10):
        print str(t).rjust(col),
    print  # end header line

    # body
    for v in range(5, 55, 5):
        print str(v).rjust(col),
        for t in range(60, -30, -10):
            wc = windchill(v, t)
            print ("%.0f" % wc).rjust(col),  # rounds to nearest integer
        print  # end row

Notes:

  • Keep the same col width for the left label and all data cells so minus signs do not shift columns.
  • If you are on Python 3, the same idea reads nicely with f-strings, e.g., print(f"{v:>{col}}", end="") and print(f"{wc:>{col}.0f}", end=""), then print() to finish each row.
  • Optional accuracy check: the wind chill formula is typically intended for temps <= 50 F and winds >= 3 mph; outside that range, consider leaving the cell blank or marking N/A.

Nice work getting the table together, , and good tip from . ’s suggestion also works; the key is using a consistent width and a newline after each row.

Recommended Answers

All 3 Replies

Hi butterflyTee,

Please check your PM inbox.

To get the numbers to line up, you are going to need to use a formatting modifier to your print statement. Try something like:

print str("%3d" %int(chill))+" ",

instead of the print statement you have in your nested for-loop. This should space the numbers properly.

thank you, it did work

Try
print '%3s ' %(str(int(chill))),

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.