[Edu-sig] Re: problem with IF

Dave Reed drlinux at columbus.rr.com
Sat Jul 24 04:09:32 CEST 2004

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


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


More information about the Edu-sig mailing list