Velocity Reviews > Small newbie question

# Small newbie question

Byte
Guest
Posts: n/a

 02-12-2006
How would I do this: Write a program that simply outputs a ramdom (in
this case) name of (for this example) a Linux distibution. Heres the
code ive tryed:

from random import uniform
from time import sleep

x = 2
while x < 5:
x = uniform(1, 5)
if x >= 1 <= 1.999: print 'SuSE'
elif x >= 2 <= 2.999: print 'Ubuntu'
elif x >= 3 <= 3.999: print 'Mandriva'
elif x >= 4 <= 4.999: print 'Fedora'
sleep(2)

-- /usr/bin/byte

Astan Chee
Guest
Posts: n/a

 02-12-2006

Byte wrote:

>How would I do this: Write a program that simply outputs a ramdom (in
>this case) name of (for this example) a Linux distibution. Heres the
>code ive tryed:
>
>from random import uniform
>from time import sleep
>
>x = 2
>while x < 5:
> x = uniform(1, 5)
> if x >= 1 <= 1.999: print 'SuSE'
> elif x >= 2 <= 2.999: print 'Ubuntu'
> elif x >= 3 <= 3.999: print 'Mandriva'
> elif x >= 4 <= 4.999: print 'Fedora'
> sleep(2)
>
>

But replace your if statement with this (which is similar), does work:
if 1 <= x <= 1.999: print 'SuSE'
elif 2 <= x <= 2.999: print 'Ubuntu'
elif 3 <= x <= 3.999: print 'Mandriva'
elif 4 <= x <= 4.999: print 'Fedora'

>
> -- /usr/bin/byte
>
>
>

Tim Parkin
Guest
Posts: n/a

 02-12-2006
Byte wrote:
> How would I do this: Write a program that simply outputs a ramdom (in
> this case) name of (for this example) a Linux distibution. Heres the
> code ive tryed:
>
> from random import uniform
> from time import sleep
>
> x = 2
> while x < 5:
> x = uniform(1, 5)
> if x >= 1 <= 1.999: print 'SuSE'
> elif x >= 2 <= 2.999: print 'Ubuntu'
> elif x >= 3 <= 3.999: print 'Mandriva'
> elif x >= 4 <= 4.999: print 'Fedora'
> sleep(2)
>
>
> -- /usr/bin/byte
>

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

Byte
Guest
Posts: n/a

 02-12-2006
Great stuff, thanks:

-- /usr/bin/byte

Paul Rubin
Guest
Posts: n/a

 02-12-2006
> while x < 5:
> x = uniform(1, 5)
> if x >= 1 <= 1.999: print 'SuSE'
> ...

Try this:

x = 27.6
if x >= 1 <= 1.999: print 'SuSE'

It prints 'SuSE' because the test is written incorrectly. You want:

if 1 <= x <= 1.999: print 'SuSE'

Dennis Lee Bieber
Guest
Posts: n/a

 02-12-2006
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 <
> ================================================== ============ <
> Overflow Page: <http://wlfraed.home.netcom.com/> <