使用嵌套循环的windchill表 [英] windchill table using nested loop
问题描述
我正在编写一个程序,该程序将打印带有windchill索引的表格.每个Windchill值应足以对应相应的行和列.
I'm writing a program that prints a table with the windchill index. Every windchill value should be adequate to a corresponding row and 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
for temp in range(-20, 70, 10):
print 2 * " ", temp,
print "\n", " " * 2, "-" * 51
#table frame, speeds
for windSpeed in range (0, 35, 5):
print windSpeed
main()
这将产生:
-20 -10 0 10 20 30 40 50 60
---------------------------------------------------
0
5
10
15
20
25
30
显然,最困难的部分是实际打印windchill值.我一直在玩代码,它只打印出系数为0和0的第一个windchill值,这些值是在程序开始时定义的.
Obviously, the hard part is to actually print the windchill values. I've been playing with the code quite a bit and it only prints out the first windchill value with its coefficients' values of 0 and 0, which are defined at the very beggining of the program.
推荐答案
由于不可能返回并更改之后已打印的行,因此您需要在打印出表格的每一行的同时计算风速.与其为每一行中出现的每种温度和风速组合重复冗长的表达式,不如创建一个单独的函数来计算它们的值,然后重复(更短)地命名它.
Since it's impossible go back and change lines already printed afterwards, you need to compute the wind-chills at the same time you print out each row of the table. Rather than repeat the longish expression for each of the temperature and wind speed combinations that appear in each row, it's better to create a separate function which calculates the value from them and then call it by (its much shorter) name repeatedly.
内置的字符串方法format()
使得以所需的方式显示所有正在计算的数据相对简单-因此花一些时间来学习它的工作方式将是非常值得的.
The built-in string method format()
makes it relatively simple to display all the data being computed the desired way—so taking the time to learn how it works would be a very worthwhile endeavor.
这是执行此操作的代码,它还会尝试遵循 PEP 8-Python代码样式指南.
Here's code which does this that also tries to follow the PEP 8 - Style Guide for Python Code.
def wind_chill(temp, wind_speed):
""" Compute wind chill given temperature and wind speed if the
temperature is 50 degrees Fahrenheit or less and the wind speed is
above 3 mph, otherwise return 'nan' (not-a-number) because it's an
undefined quantity in those situations.
"""
return (35.74 + (0.6215 * temp) - 35.75 * (wind_speed ** 0.16)
+ 0.4275 * temp * (wind_speed ** 0.16)
if temp <= 50 and wind_speed > 3 else
float('nan'))
def main():
# print table header
temps = xrange(-20, 70, 10)
num_temps = len(temps)
data = [" "] + [temp for temp in temps]
print ("{:3s}" + num_temps * " {:5d}").format(*data)
data = [" "] + num_temps * [5 * "-"]
print ("{:3s}" + num_temps * " {:5s}").format(*data)
# print table rows
row_format_string = "{:3d}" + num_temps * " {:5.1F}"
for wind_speed in xrange(0, 35, 5):
data = [wind_speed] + [wind_chill(temp, wind_speed) for temp in temps]
print row_format_string.format(*data)
main()
输出:
-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
这篇关于使用嵌套循环的windchill表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!