-
- Posts: 87
- 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?
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
Add this to the top of your code.
Edit: ...or whatever default value you want that to be.
Code: Select all
wind_chill_max = 0
My advice applies to RaspiOS only. Please mention if you use another OS.
-
- Posts: 87
- Joined: Wed Oct 25, 2017 7:34 am
- Location: Sheffield
Re: if x < y then x = y
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.
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.
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;
My advice applies to RaspiOS only. Please mention if you use another OS.
-
- Posts: 87
- Joined: Wed Oct 25, 2017 7:34 am
- Location: Sheffield
Re: if x < y then x = y
Sorry, still no joy.
Re: if x < y then x = y
That works. (Pi0W IDLE3)SurferTim wrote: ↑Mon Feb 26, 2018 12:42 pmCode: Select all
wind_chill = 0 wind_chill_max = 1 if wind_chill < wind_chill_max: wind_chill_max = wind_chill
BTW a semicolon had snuck in there but still worked anyway.
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
HTH
PeterO
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)
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
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
-
- Posts: 87
- 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?
I'm using Python IDLE2, can't think that would make too much difference?
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)
Re: if x < y then x = y
Works in IDLE 2 as well.Dangermoth wrote: ↑Mon Feb 26, 2018 12:53 pmInteresting,
I'm using Python IDLE2, can't think that would make too much difference?
-
- Posts: 87
- Joined: Wed Oct 25, 2017 7:34 am
- Location: Sheffield
Re: if x < y then x = y
No joy with that one either,PiGraham wrote: ↑Mon Feb 26, 2018 12:53 pmOr you can use a min / max function:Code: Select all
wind_chill_max = max(wind_chill, wind_chill_max)
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
It is trying to tell you that you are using wind_chill_max before it has been assigned a value.Dangermoth wrote:No joy with that one either,PiGraham wrote: ↑Mon Feb 26, 2018 12:53 pmOr you can use a min / max function:Code: Select all
wind_chill_max = max(wind_chill, wind_chill_max)
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.
My advice applies to RaspiOS only. Please mention if you use another OS.
Re: if x < y then x = y
Dangermoth wrote: ↑Mon Feb 26, 2018 1:00 pmNo joy with that one either,PiGraham wrote: ↑Mon Feb 26, 2018 12:53 pmOr you can use a min / max function:Code: Select all
wind_chill_max = max(wind_chill, wind_chill_max)
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
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
-
- Posts: 87
- 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.
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
Yes. That is what you want by the looks of your code.Dangermoth wrote: ↑Mon Feb 26, 2018 1:07 pmSo 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.
My advice applies to RaspiOS only. Please mention if you use another OS.
Re: if x < y then x = y
It would be useful to know why any of these codes has a problem.PeterO wrote: ↑Mon Feb 26, 2018 1:06 pmDangermoth wrote: ↑Mon Feb 26, 2018 1:00 pmNo joy with that one either,PiGraham wrote: ↑Mon Feb 26, 2018 12:53 pmOr you can use a min / max function:Code: Select all
wind_chill_max = max(wind_chill, wind_chill_max)
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
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
NO, it's the "global wind_chill_max" that is important.Dangermoth wrote: ↑Mon Feb 26, 2018 1:07 pmSo 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.
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
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
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?
The variable is a global scope and initialised.
Who sees an error?
SurferTim wrote: ↑Mon Feb 26, 2018 12:42 pmCode: 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.
Re: if x < y then x = y
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
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
-
- Posts: 87
- 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.
Re: if x < y then x = y
So the code you posted works for you as it is?Dangermoth wrote: ↑Mon Feb 26, 2018 1:15 pmSorry, forgot to mention it is indeed within a function.
-
- Posts: 87
- Joined: Wed Oct 25, 2017 7:34 am
- Location: Sheffield
Re: if x < y then x = y
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.PiGraham wrote: ↑Mon Feb 26, 2018 1:17 pmSo the code you posted works for you as it is?Dangermoth wrote: ↑Mon Feb 26, 2018 1:15 pmSorry, forgot to mention it is indeed within a function.
Hope that kind of makes sense.
Re: if x < y then x = y
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
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)
-
- Posts: 87
- 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.
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.