Small newbie question

B

Byte

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)

It dosnt work: only keep printing SuSE. Please help,

Thanks in advance,
-- /usr/bin/byte
 
A

Astan Chee

Byte said:
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'
 
T

Tim Parkin

Byte said:
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)

It dosnt work: only keep printing SuSE. Please help,

Thanks in advance,
-- /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
 
P

Paul Rubin

while x < 5:
x = uniform(1, 5)
if x >= 1 <= 1.999: print 'SuSE'
...
It dosnt work: only keep printing SuSE. Please help,

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'
 
D

Dennis Lee Bieber

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

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


said:
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 --
> ============================================================== <
> (e-mail address removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (e-mail address removed) | Bestiaria Support Staff <
> ============================================================== <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top