CarnivorousMonster - 6 months ago 34

Python Question

I started python only a few months ago and I have been given a task to use a nested loops to calculate wind chill.

I have most of the code completed (I think), however the equation is not working the way it want it to work.

Here is my code at the moment:

`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()

This comes out with this:

`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

I understand why 13 is spited out, it's because if the temp and wind are 0, it comes to the answer 13.12. But if I do the range for the definition of temp and wind, it will not accept a list for the definition.

How do I make it so that the wind chill isn't 13, but the answer which the equation should spit out.

e.g. comes out with this:

`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.

Thank you very much!

If what I have put is simple, and I should just google it, I tried googling it but it makes a table, instead of statements.

Carny.

Answer

You must define an actual *function*. What you have is just an assignment:

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

Basically, `temp`

and `wind`

are used with their current values to resolve the expression. `windChill`

then just gets that fixed value assigned to it.

A function would look like this:

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

Then *call* that function in your loop:

```
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)
```

Alternatively, you can move the definition of `windChill`

into both loops, so it gets reevaluated every time.

```
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)
```