Page 1 of 2

if x < y then x = y

Posted: Mon Feb 26, 2018 12:28 pm
by Dangermoth
OK, stupid question of the day.

I want to store the maximum (or is it minimum) wind chill figure on my weather station. To do this in 'C' I can do something like:


if (wind_chill < wind_chill_max) wind_chill_max = wind_chill;





I can't find a way to do it in python, I've tried:



if wind_chill < wind_chill_max:

wind_chill_max = wind_chill




but this just gives an error of 'wind_chill_max referenced before assignment' ????

Any idea how I can accomplish this?

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 12:35 pm
by SurferTim
Add this to the top of your code.

Code: Select all

wind_chill_max = 0
Edit: ...or whatever default value you want that to be.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 12:38 pm
by Dangermoth
Thanks for the quick reply.

I have already done this, and copy and pasted to make sure there are no spelling differences.


Edit
I have done this routine several times on the Arduino that is reading the analog signals ( min / max temperature ect) but the wind chill is being calculated on the Pi.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 12:42 pm
by SurferTim

Code: Select all

wind_chill = 0
wind_chill_max = 1

if wind_chill < wind_chill_max: 
	wind_chill_max = wind_chill;

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 12:47 pm
by Dangermoth
Sorry, still no joy.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 12:48 pm
by PiGraham
SurferTim wrote:
Mon Feb 26, 2018 12:42 pm

Code: Select all

wind_chill = 0
wind_chill_max = 1

if wind_chill < wind_chill_max: 
	wind_chill_max = wind_chill
That works. (Pi0W IDLE3)

BTW a semicolon had snuck in there but still worked anyway.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 12:50 pm
by PeterO
If your code is in a function then you'll need to make wind_chill_max a global

Code: Select all

wind_chill = 8
wind_chill_max = 0

def fn():
    global wind_chill_max
    if wind_chill > wind_chill_max:
        wind_chill_max = wind_chill

fn()
print("wind_chill=",wind_chill,"wind_chill_max=",wind_chill_max)
HTH
PeterO

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 12:53 pm
by Dangermoth
Interesting,

I'm using Python IDLE2, can't think that would make too much difference?

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 12:53 pm
by PiGraham
Or you can use a min / max function:

Code: Select all

wind_chill_max = max(wind_chill, wind_chill_max)

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 12:57 pm
by PiGraham
Dangermoth wrote:
Mon Feb 26, 2018 12:53 pm
Interesting,

I'm using Python IDLE2, can't think that would make too much difference?
Works in IDLE 2 as well.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:00 pm
by Dangermoth
PiGraham wrote:
Mon Feb 26, 2018 12:53 pm
Or you can use a min / max function:

Code: Select all

wind_chill_max = max(wind_chill, wind_chill_max)
No joy with that one either,

I've just read the error again and it is saying;

Unbound Local Error: Local Variable 'wind_chill_max' referenced before assignment

No idea what it means but its trying to tell me something.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:03 pm
by SurferTim
Dangermoth wrote:
PiGraham wrote:
Mon Feb 26, 2018 12:53 pm
Or you can use a min / max function:

Code: Select all

wind_chill_max = max(wind_chill, wind_chill_max)
No joy with that one either,

I've just read the error again and it is saying;

Unbound Local Error: Local Variable 'wind_chill_max' referenced before assignment

No idea what it means but its trying to tell me something.
It is trying to tell you that you are using wind_chill_max before it has been assigned a value.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:06 pm
by PeterO
Dangermoth wrote:
Mon Feb 26, 2018 1:00 pm
PiGraham wrote:
Mon Feb 26, 2018 12:53 pm
Or you can use a min / max function:

Code: Select all

wind_chill_max = max(wind_chill, wind_chill_max)
No joy with that one either,

I've just read the error again and it is saying;

Unbound Local Error: Local Variable 'wind_chill_max' referenced before assignment

No idea what it means but its trying to tell me something.

It's trying to tell you that my code above is the answer ! ;)

PeterO

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:07 pm
by Dangermoth
So if I type "wind_chill_max = 0" at the top of the code does this mean its a global value and not local?

Sorry for the stupid question, I'm still trying to get my head around Python after using C for many years.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:08 pm
by SurferTim
Dangermoth wrote:
Mon Feb 26, 2018 1:07 pm
So if I type "wind_chill_max = 0" at the top of the code does this mean its a global value and not local?

Sorry for the stupid question, I'm still trying to get my head around Python after using C for many years.
Yes. That is what you want by the looks of your code.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:09 pm
by PiGraham
PeterO wrote:
Mon Feb 26, 2018 1:06 pm
Dangermoth wrote:
Mon Feb 26, 2018 1:00 pm
PiGraham wrote:
Mon Feb 26, 2018 12:53 pm
Or you can use a min / max function:

Code: Select all

wind_chill_max = max(wind_chill, wind_chill_max)
No joy with that one either,

I've just read the error again and it is saying;

Unbound Local Error: Local Variable 'wind_chill_max' referenced before assignment

No idea what it means but its trying to tell me something.

It's trying to tell you that my code above is the answer ! ;)

PeterO
It would be useful to know why any of these codes has a problem.
Are you suggesting that putting it in a function is necessary? The original code runs OK fo me. Doesn't it for you?

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:10 pm
by PeterO
Dangermoth wrote:
Mon Feb 26, 2018 1:07 pm
So if I type "wind_chill_max = 0" at the top of the code does this mean its a global value and not local?

Sorry for the stupid question, I'm still trying to get my head around Python after using C for many years.
NO, it's the "global wind_chill_max" that is important.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:12 pm
by PiGraham
Rechecking, this code copied and pasted into a file in IDLE2 runs just fine, as I would expect.
The variable is a global scope and initialised.

Who sees an error?

SurferTim wrote:
Mon Feb 26, 2018 12:42 pm

Code: Select all

wind_chill = 0
wind_chill_max = 1

if wind_chill < wind_chill_max: 
	wind_chill_max = wind_chill;

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:12 pm
by PeterO
PiGraham wrote:
Mon Feb 26, 2018 1:09 pm
It would be useful to know why any of these codes has a problem.
Are you suggesting that putting it in a function is necessary? The original code runs OK fo me. Doesn't it for you?
Note the error message says that wind_chill_max is a local variable so the OP's code must have been inside a function.
PeterO

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:15 pm
by Dangermoth
Sorry, forgot to mention it is indeed within a function.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:17 pm
by PiGraham
Dangermoth wrote:
Mon Feb 26, 2018 1:15 pm
Sorry, forgot to mention it is indeed within a function.
So the code you posted works for you as it is?

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:25 pm
by Dangermoth
PiGraham wrote:
Mon Feb 26, 2018 1:17 pm
Dangermoth wrote:
Mon Feb 26, 2018 1:15 pm
Sorry, forgot to mention it is indeed within a function.
So the code you posted works for you as it is?
I already have a function that reads the wind speed and temperature from the arduino, calculates the wind chill and prints it on a Pygame screen, as I don't know how to read variables between functions I popped the 'max wind chill' bit of code inside the same function.

Hope that kind of makes sense.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 1:37 pm
by PiGraham
Dangermoth wrote:
Mon Feb 26, 2018 1:25 pm

Hope that kind of makes sense.
It's quite useful to post code that actually exhibits the problem.
The code posted by surfer tim worked. I guess you didn't try that.

Glad you have it working now. Good job PeterO was on the case.

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 2:01 pm
by PingoBags
Something like this?

Code: Select all

wind_chill = 0

def MaxWindChill(current_chill):
  global wind_chill
  wind_chill = current_chill if wind_chill < current_chill else wind_chill

Code: Select all

MaxWindChill(2.2)#2.2 celcius chill as an example
print(wind_chill)

Re: if x < y then x = y

Posted: Mon Feb 26, 2018 2:04 pm
by Dangermoth
Sorry I got called away,

It's still not working, I'm kind of thinking I should do the calculation and comparison on the Arduino as I do with the max / min temperature, this looks to be a lot more in depth using Python than I thought.