THE WINDCHILL TABLE IS RUNNING, BUT THE NUMBER IS NOT LINNING UP STRAIGHT. CAN YOU PLEASE HELP ME, THANKS

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 (-20, 70, 10):
            chill = windchill(x,y)
            print str(int(chill)) + " ",
       



main()

:mad: :evil: :cry: :o

Dani AI

Generated

Two separate things are causing the output to look "not lined up": the speed is being printed on its own line, and the numbers printed after it vary in width. is right to suggest padding the numbers — that will make each column a fixed width — but the outer print also needs to stay on the same line (or you should build a full row string and print it in one go). Also note that int() simply truncates the fractional part; use rounding if you want the usual nearest-integer behavior.

A simple, robust approach is to build each output row as a string and format each cell to a fixed width. This example uses Python 3-style formatting and rounds the wind-chill values for display:

for speed in range(5, 55, 5):
    row = "{:>3}".format(speed)
    for temp in range(-20, 70, 10):
        wc = windchill(speed, temp)
        row += " {:>5.0f}".format(wc)   # width 5, rounded
    print(row)

If the environment is Python 2, the same idea works with old-style formatting:

for speed in range(5, 55, 5):
    row = "%3d" % speed
    for temp in range(-20, 70, 10):
        row += " %5.0f" % windchill(speed, temp)
    print row

Practical tips: pick a field width that fits negative numbers and the largest expected value (e.g., 5 to allow a minus sign plus three digits), use rounding (round() or :.0f) rather than int() if you want conventional rounding, and align the header using the same widths so columns line up. Building the row string is usually easier and less error-prone than juggling print statements that rely on trailing commas or end=.


print str(int(chill)) + " ",

you can try this statement instead :

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.