[Edu-sig] problem with IF

David Handy david at handysoftware.com
Fri Jul 23 06:25:20 CEST 2004


> 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 at joeys.org
ken at wyverntech.com

_______________________________________________
Edu-sig mailing list
Edu-sig at python.org
http://mail.python.org/mailman/listinfo/edu-sig



More information about the Edu-sig mailing list