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

if x < y then x = y

Mon Feb 26, 2018 12:28 pm

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

Mon Feb 26, 2018 12:35 pm

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.

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

Re: if x < y then x = y

Mon Feb 26, 2018 12:38 pm

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

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;

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

Re: if x < y then x = y

Mon Feb 26, 2018 12:47 pm

Sorry, still no joy.

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

Re: if x < y then x = y

Mon Feb 26, 2018 12:48 pm

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.

User avatar
PeterO
Posts: 4722
Joined: Sun Jul 22, 2012 4:14 pm

Re: if x < y then x = y

Mon Feb 26, 2018 12:50 pm

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: 59
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

Re: if x < y then x = y

Mon Feb 26, 2018 12:53 pm

Interesting,

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

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

Re: if x < y then x = y

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)

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

Re: if x < y then x = y

Mon Feb 26, 2018 12:57 pm

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: 59
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

Re: if x < y then x = y

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.

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

Re: if x < y then x = y

Mon Feb 26, 2018 1:03 pm

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.

User avatar
PeterO
Posts: 4722
Joined: Sun Jul 22, 2012 4:14 pm

Re: if x < y then x = y

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
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: 59
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

Re: if x < y then x = y

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.

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

Re: if x < y then x = y

Mon Feb 26, 2018 1:08 pm

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: 3546
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: if x < y then x = y

Mon Feb 26, 2018 1:09 pm

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?

User avatar
PeterO
Posts: 4722
Joined: Sun Jul 22, 2012 4:14 pm

Re: if x < y then x = y

Mon Feb 26, 2018 1:10 pm

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: 3546
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: if x < y then x = y

Mon Feb 26, 2018 1:12 pm

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.

User avatar
PeterO
Posts: 4722
Joined: Sun Jul 22, 2012 4:14 pm

Re: if x < y then x = y

Mon Feb 26, 2018 1:12 pm

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: 59
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

Re: if x < y then x = y

Mon Feb 26, 2018 1:15 pm

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

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

Re: if x < y then x = y

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?

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

Re: if x < y then x = y

Mon Feb 26, 2018 1:25 pm

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: 3546
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: if x < y then x = y

Mon Feb 26, 2018 1:37 pm

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
Location: NS, Canada

Re: if x < y then x = y

Mon Feb 26, 2018 2:01 pm

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: 59
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

Re: if x < y then x = y

Mon Feb 26, 2018 2:04 pm

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.

Return to “Beginners”