
hello all, I have been trying to write a program to display the reading from a weather station. I was trying to change the direction of the wind from degrees to points of compass. Have tried various if.. elif.. but it seems to take the last option in the list regardless of the value of the degrees. # let's put in points of compass, instead of degrees dirnPoint = "N" print dirn if dirn >= 22: dirnPoint = "NE" print dirnPoint if dirn >77 : dirnPoint = "E" print dirnPoint if dirn >112 : dirnPoint = "SE" print dirnPoint if dirn > 157 : dirnPoint = "S" print dirnPoint if dirn > 202 : dirnPoint = "SW" print dirnPoint if dirn > 247: dirnPoint = "W" print dirnPoint if dirn > 292: dirnPoint = "NW" print dirnPoint if dirn > 345: dirnPoint = "N" print dirnPoint print dirn I added the prints to follow the progress through the chioces. Below is the printout from a sample direction;301 NE E SE S SW W NW N 301 To my way of thinking, it should have stopped at "W" but it seems to be getting TRUE for > 292 and > 345. Any ideas, please. Thanks.. ken Wallwork Computing Studies Co-ordinator St Joseph's College Hunters Hill NSW Australia ken@joeys.org ken@wyverntech.com

hello all, I have been trying to write a program to display the reading from a weather station. I was trying to change the direction of the wind from degrees to points of compass. Have tried various if.. elif.. but it seems to take the last option in the list regardless of the value of the degrees.
The indentation of your program in the email was all messed up. My guess is that you wrote your program with a text editor that mixes spaces and tabs. That is a Very Bad Thing, especially with Python programs. The safest thing is to set up your editor so that it uses spaces instead of tab characters to indent. The IDLE editor that comes with Python does this by default. Here is my guess at how your program originally looked: # let's put in points of compass, instead of degrees dirnPoint = "N" print dirn if dirn >= 22: dirnPoint = "NE" print dirnPoint if dirn >77 : dirnPoint = "E" print dirnPoint if dirn >112 : dirnPoint = "SE" print dirnPoint if dirn > 157 : dirnPoint = "S" print dirnPoint if dirn > 202 : dirnPoint = "SW" print dirnPoint if dirn > 247: dirnPoint = "W" print dirnPoint if dirn > 292: dirnPoint = "NW" print dirnPoint if dirn > 345: dirnPoint = "N" print dirnPoint print dirn After I fixed the indentation on your program, and ran it with dirn=301, I got different results than you did: 301 NE E SE S SW W NW NW 301 This correctly says that 301 degrees is NW.
To my way of thinking, it should have stopped at "W" but it seems to be getting TRUE for > 292 and > 345.
No, NW is correct for 301 degrees. 301 is > 292.
Any ideas, please.
I suspect that the mixed tab and space characters that caused the bad indentation in the email also caused Python to "misinterpret" your if statements. Try running Python with the -tt argument to do more checking for inconsistent tab usage, like this: python -tt compass.py To convert tabs to spaces in existing program files, try the untabify.py script that comes with Python. On Windows it is at: C:\Python23\Tools\Scripts\untabify.py or in the Python source code it is at: Tools/scripts/untabify.py Going forward, I strongly recommend that you set up your editor to always indent your Python programs with spaces instead of tab characters. See the Python style guide at: http://www.python.org/peps/pep-0008.html especially the section labled "Tabs or Spaces?", under "Code lay-out". I hope this helps - David H. On Fri, 23 Jul 2004, ken wrote: hello all, I have been trying to write a program to display the reading from a weather station. I was trying to change the direction of the wind from degrees to points of compass. Have tried various if.. elif.. but it seems to take the last option in the list regardless of the value of the degrees. # let's put in points of compass, instead of degrees dirnPoint = "N" print dirn if dirn >= 22: dirnPoint = "NE" print dirnPoint if dirn >77 : dirnPoint = "E" print dirnPoint if dirn >112 : dirnPoint = "SE" print dirnPoint if dirn > 157 : dirnPoint = "S" print dirnPoint if dirn > 202 : dirnPoint = "SW" print dirnPoint if dirn > 247: dirnPoint = "W" print dirnPoint if dirn > 292: dirnPoint = "NW" print dirnPoint if dirn > 345: dirnPoint = "N" print dirnPoint print dirn I added the prints to follow the progress through the chioces. Below is the printout from a sample direction;301 NE E SE S SW W NW N 301 To my way of thinking, it should have stopped at "W" but it seems to be getting TRUE for > 292 and > 345. Any ideas, please. Thanks.. ken Wallwork Computing Studies Co-ordinator St Joseph's College Hunters Hill NSW Australia ken@joeys.org ken@wyverntech.com _______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig

Ken, I think you should check to see if the bearing is between two values as follows: #!/usr/bin/env python def dirnPoint(dirn): if dirn >= 345 and dirn < 22: return "N" elif dirn >= 22 and dirn < 77: return "NE" elif dirn >= 77 and dirn < 112: return "E" elif dirn >= 112 and dirn < 157: return "SE" elif dirn >= 157 and dirn < 202: return "S" elif dirn >= 202 and dirn < 247: return "SW" elif dirn >= 247 and dirn < 292: return "W" elif dirn >= 292 and dirn < 345: return "NW" inp = int(raw_input("Enter Bearing in degrees: ")) dir = dirnPoint(inp) print "%d degrees is %s" % (inp, dir) On Thursday 22 July 2004 21:49, ken wrote:
hello all, I have been trying to write a program to display the reading from a weather station. I was trying to change the direction of the wind from degrees to points of compass. Have tried various if.. elif.. but it seems to take the last option in the list regardless of the value of the degrees.

Thanks to all who responded to my problem. I had tried to use a text string for the right side of the comparison and originally a between values, using AND set of decisions. Also the original set of IFs was using elif but all had failed. The thing tht finally worked was making sure that the numeric for wind dirn was as an int. I thought I had used it as an int() previously and didn't look back at that 8-( Anyway, a simple dirn = int(dirn) has fixed the problem 8-) ken W Rick Holbert wrote:
Ken,
I think you should check to see if the bearing is between two values as follows:
#!/usr/bin/env python
def dirnPoint(dirn): if dirn >= 345 and dirn < 22: return "N" elif dirn >= 22 and dirn < 77: return "NE" elif dirn >= 77 and dirn < 112: return "E" elif dirn >= 112 and dirn < 157: return "SE" elif dirn >= 157 and dirn < 202: return "S" elif dirn >= 202 and dirn < 247: return "SW" elif dirn >= 247 and dirn < 292: return "W" elif dirn >= 292 and dirn < 345: return "NW"
inp = int(raw_input("Enter Bearing in degrees: "))
dir = dirnPoint(inp)
print "%d degrees is %s" % (inp, dir)
On Thursday 22 July 2004 21:49, ken wrote:
hello all, I have been trying to write a program to display the reading from a weather station. I was trying to change the direction of the wind from degrees to points of compass. Have tried various if.. elif.. but it seems to take the last option in the list regardless of the value of the degrees.
_______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig -- Scanned by The Sheriff - http://www.isheriff.com/

Maybe this isn't really the forum, but I can't resist a couple quick comments on this example. Since this is a multiway decision (that is, 8 mutually exclusive outcomes based on a single decision value), elif is definitely appropriate but having two conditions on each clause is redundant and often leads to errors. In fact there is an error in the code below, since it can nver return 'N' (dirn can't be both >= 345 and < 22). Of course, North is a bit of a special case; one way to do that is to split "N" into its two cases: if dirn < 22: return "N" elif dirn < 77: return "NE" elif dirn < 112: return "E" elif dirn < 157: return "SE" etc... elif dirn < 345: return "NW" else: return "N" # anything >= 345 Also, if one is getting an int as input, you may as well use the input function (save raw_input for strings), then you won't accidently use a string as an int. dirn = input("Enter bearing in degrees: ") Of course, if this were embedded in a GUI, chances are you would be forced to get a string and do the conversion. --John ps. I can't help thinking there may be a more elegant way of doing the bearing conversion. Rick Holbert wrote:
Ken,
I think you should check to see if the bearing is between two values as follows:
#!/usr/bin/env python
def dirnPoint(dirn): if dirn >= 345 and dirn < 22: return "N" elif dirn >= 22 and dirn < 77: return "NE" elif dirn >= 77 and dirn < 112: return "E" elif dirn >= 112 and dirn < 157: return "SE" elif dirn >= 157 and dirn < 202: return "S" elif dirn >= 202 and dirn < 247: return "SW" elif dirn >= 247 and dirn < 292: return "W" elif dirn >= 292 and dirn < 345: return "NW"
inp = int(raw_input("Enter Bearing in degrees: "))
dir = dirnPoint(inp)
print "%d degrees is %s" % (inp, dir)
On Thursday 22 July 2004 21:49, ken wrote:
hello all, I have been trying to write a program to display the reading from a weather station. I was trying to change the direction of the wind from degrees to points of compass. Have tried various if.. elif.. but it seems to take the last option in the list regardless of the value of the degrees.
_______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig

On Friday 23 July 2004 20:42, John Zelle wrote:
Maybe this isn't really the forum, but I can't resist a couple quick comments on this example.
Since this is a multiway decision (that is, 8 mutually exclusive outcomes based on a single decision value), elif is definitely appropriate but having two conditions on each clause is redundant and often leads to errors. In fact there is an error in the code below, since it can nver return 'N' (dirn can't be both >= 345 and < 22). Of course, North is a bit of a special case; one way to do that is to split "N" into its two cases:
if dirn < 22: return "N" elif dirn < 77: return "NE" elif dirn < 112: return "E" elif dirn < 157: return "SE" etc... elif dirn < 345: return "NW" else: return "N" # anything >= 345
Also, if one is getting an int as input, you may as well use the input function (save raw_input for strings), then you won't accidently use a string as an int.
dirn = input("Enter bearing in degrees: ")
Of course, if this were embedded in a GUI, chances are you would be forced to get a string and do the conversion.
--John
<snip> If all the directions had the same number of degrees (which I would think they should, but they didn't appear to in the example), it would make sense to me to divide by that number (after adding a constant as necessary) and using it as an index into a tuple like ('N', 'NE', 'E', 'SE', etc). Dave

Thanks for the input John. Another idea for north is to test if it is >= 345 or < 22. Also, I've recommended your book to the CIS department here at OSU. Sincerely, Rick On Friday 23 July 2004 20:42, John Zelle wrote:
Maybe this isn't really the forum, but I can't resist a couple quick comments on this example.
Since this is a multiway decision (that is, 8 mutually exclusive outcomes based on a single decision value), elif is definitely appropriate but having two conditions on each clause is redundant and often leads to errors. In fact there is an error in the code below, since it can nver return 'N' (dirn can't be both >= 345 and < 22). Of course, North is a bit of a special case; one way to do that is to split "N" into its two cases:
if dirn < 22: return "N" elif dirn < 77: return "NE" elif dirn < 112: return "E" elif dirn < 157: return "SE" etc... elif dirn < 345: return "NW" else: return "N" # anything >= 345
participants (5)
-
Dave Reed
-
David Handy
-
John Zelle
-
ken
-
Rick Holbert