python - windchill table using nested loop -
i'm writing programme prints table windchill index. every windchill value should adequate corresponding row , column.
def main(): windspeed = 0 temp = 0 windchill = 35.74 + (0.6215 * temp) - 35.75 * (windspeed ** 0.16) \ + 0.4275 * temp * (windspeed ** 0.16) # table frame, temps temp in range(-20, 70, 10): print 2 * " ", temp, print "\n", " " * 2, "-" * 51 #table frame, speeds windspeed in range (0, 35, 5): print windspeed main()
this produces:
-20 -10 0 10 20 30 40 50 60 --------------------------------------------------- 0 5 10 15 20 25 30
obviously, hard part print windchill values. i've been playing code quite bit , prints out first windchill value coefficients' values of 0 , 0, defined @ beggining of program.
since it's impossible go , alter lines printed afterwards, need compute wind-chills @ same time print out each row of table. rather repeat longish each of temperature , wind speed combinations on appear on each row, easier create separate function calculates value them , phone call necessary number of times.
the built-in string method format()
makes relatively simple display info beingness computed desired way -- it's worthwhile larn how works.
here's code while , follows pep-8 python coding style guidelines.
def wind_chill(temp, wind_speed): """ compute wind chill given temperature , wind speed if temperature 50 degrees fahrenheit or less , wind speed above 3 mph, otherwise homecoming 'nan' (not-a-number) because it's undefined otherwise. """ homecoming (35.74 + (0.6215 * temp) - 35.75 * (wind_speed ** 0.16) + 0.4275 * temp * (wind_speed ** 0.16) if temp <= 50 , wind_speed > 3 else float('nan')) def main(): # print table header temps = xrange(-20, 70, 10) num_temps = len(temps) info = [" "] + [temp temp in temps] print ("{:3s}" + num_temps * " {:5d}").format(*data) info = [" "] + num_temps * [5 * "-"] print ("{:3s}" + num_temps * " {:5s}").format(*data) # print table rows row_format_string = "{:3d}" + num_temps * " {:5.1f}" wind_speed in xrange(0, 35, 5): info = [wind_speed] + [wind_chill(temp, wind_speed) temp in temps] print row_format_string.format(*data) main()
output:
class="lang-none prettyprint-override"> -20 -10 0 10 20 30 40 50 60 ----- ----- ----- ----- ----- ----- ----- ----- ----- 0 nan nan nan nan nan nan nan nan nan 5 -34.0 -22.3 -10.5 1.2 13.0 24.7 36.5 48.2 nan 10 -40.7 -28.3 -15.9 -3.5 8.9 21.2 33.6 46.0 nan 15 -45.0 -32.2 -19.4 -6.6 6.2 19.0 31.8 44.6 nan 20 -48.2 -35.1 -22.0 -8.9 4.2 17.4 30.5 43.6 nan 25 -50.8 -37.5 -24.1 -10.7 2.6 16.0 29.4 42.8 nan 30 -53.0 -39.4 -25.9 -12.3 1.3 14.9 28.5 42.0 nan
python python-2.7 nested-loops
No comments:
Post a Comment