On Sun, 12 Feb 2006 20:17:47 +0000, Tim Parkin <(E-Mail Removed)>

declaimed the following in comp.lang.python:

> Byte wrote:
<snip>

I'm sneaking in on this response for a reason... Whilst the answer

given meets the problem definition, I don't think it helps clear up what

was wrong with the original try...

> > if x >= 1 <= 1.999: print 'SuSE'
"1 <= 1.999" is always TRUE! Oh, "x >= 1" is ALSO TRUE for any value

of X, given that you limited the range in 1..5! So this statement is

always executed.

Chained comparisons "a < b < c" (for example) are the equivalent of

"(a < b) AND (b < c)". You have, then "(x >= 1) AND (1 <= 1.999)", this

is NOT the same as "(x >= 1) AND (x <= 1.9999)"

Compare:

>>> x = 3.14159

>>> print x >= 1 <= 1.9999
True

>>> print 1 <= x <= 1.9999
False

>>>

>>> if 1 <= x <= 1.9999: print "SuSE"
.... elif 2 <= x <= 2.9999: print "What happens if x is 1.99995?"

.... elif 3 <= x <= 3.9999: print "Mandriva"

.... elif 4 <= x <= 4.9999: print "What happens if x is 4.99995?"

....

Mandriva

>>>
For this style of selection, it would be better to use:

if 1 <= x < 2: ... #note the <= vs <

elif 2 <= x < 3: ... #etc. Your selection leaves a gap between

# the n.9999 and n+1 values.

<snip>

>

> import random

> dist = ['suse','ubuntu','mandriva','fedora']

> random.choice(dist)

>

> is that ok?

>

> Tim Parkin

>

> [1] http://www.python.org/doc/lib/module-random.html
--

> ================================================== ============ <

> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <

> (E-Mail Removed) | Bestiaria Support Staff <

> ================================================== ============ <

> Home Page: <http://www.dm.net/~wulfraed/> <

> Overflow Page: <http://wlfraed.home.netcom.com/> <