2016-10-10 6 views
0

私は数ヶ月前にpythonを始めました。風の寒さを計算するためにネストされたループを使用するタスクが与えられました。WindchillネストされたループPythonコードの式

私はコードの大部分を完成しましたが(私は思いますが)、式は機能しないと考えています。ここで

は、現時点では私のコードです:これはこれで出てくる

def main(): 
temp = 0 
wind = 0 
windChill = 13.12 + (.6215 * temp) - (11.37 * wind ** 770.16) + (.3965 * temp * wind **0.16) 

for temp in range(-35,15,5): 
    print 'temperature is %d' % temp 
    for wind in range(0,85,5): 
     answer = float(windChill) 
     print 'wind is %d calculated wind chill is: %d' % (wind, answer) 

main() 

:13がアウトspitedている理由私は理解して

temperature is -35 
wind is 0 calculated wind chill is: 13 
wind is 5 calculated wind chill is: 13 
wind is 10 calculated wind chill is: 13 
wind is 15 calculated wind chill is: 13 
wind is 20 calculated wind chill is: 13 
wind is 25 calculated wind chill is: 13 
wind is 30 calculated wind chill is: 13 
wind is 35 calculated wind chill is: 13 
wind is 40 calculated wind chill is: 13 
wind is 45 calculated wind chill is: 13 
wind is 50 calculated wind chill is: 13 
wind is 55 calculated wind chill is: 13 
wind is 60 calculated wind chill is: 13 
wind is 65 calculated wind chill is: 13 
wind is 70 calculated wind chill is: 13 
wind is 75 calculated wind chill is: 13 
wind is 80 calculated wind chill is: 13 
temperature is -30 
wind is 0 calculated wind chill is: 13 

TEMPと風が0であるかのため、それは、ですそれは答え13.12に来る。しかし、私が気温と風の定義の範囲を行うと、それは定義のリストを受け入れません。

風の寒さは13ではなく、式が吐き出されるようにします。

temperature is -35 
wind is 0 calculated wind chill is: -8.63 
wind is 5 calculated wind chill is: -41.29 
wind is 10 calculated wind chill is: -45.12 
etc. 
etc. 

ありがとうございました!

私が置いたものがシンプルで、私はちょうどそれをgoogleする必要があります、私はそれを検索しようとしましたが、ステートメントの代わりにテーブルを作成します。

カーニー。

+0

あなたは同じ 'windChill'値を何度も繰り返し使用しています。期待どおりの価値を得るためには、そこにある計算を適用する必要がありますか? – idjaw

+0

あなたのコードをもう一度見て、 'windChill'の計算があなたのループ内にあるべきではありませんか? – idjaw

+0

ええ、下のmistermiyagiは私を修正しました。それを見ると、明らかになったようですが、私は何の手がかりも持たない年齢のためにそれを見ていました。そんなばかげた質問を申し訳ありません。ご協力ありがとうございました。 – CarnivorousMonster

答えて

1

実際のファンクションを定義する必要があります。基本的には、tempwind式を解決するためにそれらの現在の値で使用されている

temp = 0 
wind = 0 
windChill = 13.12 + (.6215 * temp) - (11.37 * wind ** 770.16) + (.3965 * temp * wind **0.16) 
print(windChill) # 13.12 

:あなたが持っていることはただの割り当てです。 windChillは、その固定値を割り当てられます。

関数は次のようになります。

def windChill(temp, wind): 
    return 13.12 + (.6215 * temp) - (11.37 * wind ** 770.16) + (.3965 * temp * wind **0.16) 

次に、あなたのループで機能コール

for temp in range(-35,15,5): 
    print 'temperature is %d' % temp 
    for wind in range(0,85,5): 
     answer = float(windChill(temp, wind)) # note me! 
     print 'wind is %d calculated wind chill is: %d' % (wind, answer) 

また、あなたは両方のループにwindChillの定義を移動することができますので、毎回再評価される。

for temp in range(-35,15,5): 
    print 'temperature is %d' % temp 
    for wind in range(0,85,5): 
     answer = 13.12 + (.6215 * temp) - (11.37 * wind ** 770.16) + (.3965 * temp * wind **0.16) 
     print 'wind is %d calculated wind chill is: %d' % (wind, answer) 
+0

Ohhhhhh、ダム私はとてもばかげている!どうもありがとうございます!申し訳ありませんが、本当にばかげた質問のように聞こえる場合、私はこの日を楽しみにしていて、何の進歩も見せていませんでした。 ありがとうsooooooo! – CarnivorousMonster

関連する問題