## if x < y then x = y

Dangermoth
Posts: 83
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

### if x < y then x = y

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?

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

### Re: if x < y then x = y

Code: Select all

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

Dangermoth
Posts: 83
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

### Re: if x < y then x = y

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.
Last edited by Dangermoth on Mon Feb 26, 2018 12:44 pm, edited 1 time in total.

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

### Re: if x < y then x = y

Code: Select all

``````wind_chill = 0
wind_chill_max = 1

if wind_chill < wind_chill_max:
wind_chill_max = wind_chill;
``````

Dangermoth
Posts: 83
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

### Re: if x < y then x = y

Sorry, still no joy.

PiGraham
Posts: 4034
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

### Re: if x < y then x = y

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.

PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

### Re: if x < y then x = y

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Dangermoth
Posts: 83
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

### Re: if x < y then x = y

Interesting,

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

PiGraham
Posts: 4034
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

### Re: if x < y then x = y

Or you can use a min / max function:

Code: Select all

``wind_chill_max = max(wind_chill, wind_chill_max)``

PiGraham
Posts: 4034
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

### Re: if x < y then x = y

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.

Dangermoth
Posts: 83
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

### Re: if x < y then x = y

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.

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

### Re: if x < y then x = y

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.

PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

### Re: if x < y then x = y

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Dangermoth
Posts: 83
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

### Re: if x < y then x = y

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.

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

### Re: if x < y then x = y

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.

PiGraham
Posts: 4034
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

### Re: if x < y then x = y

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?

PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

### Re: if x < y then x = y

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.
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PiGraham
Posts: 4034
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

### Re: if x < y then x = y

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;
``````
Last edited by PiGraham on Mon Feb 26, 2018 1:13 pm, edited 1 time in total.

PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

### Re: if x < y then x = y

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Dangermoth
Posts: 83
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

### Re: if x < y then x = y

Sorry, forgot to mention it is indeed within a function.

PiGraham
Posts: 4034
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

### Re: if x < y then x = y

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?

Dangermoth
Posts: 83
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

### Re: if x < y then x = y

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.

PiGraham
Posts: 4034
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

### Re: if x < y then x = y

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.

PingoBags
Posts: 47
Joined: Mon Nov 06, 2017 10:53 pm

### Re: if x < y then x = y

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

Dangermoth
Posts: 83
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

### Re: if x < y then x = y

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.