Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Small newbie question

Reply
Thread Tools

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)

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

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

 
Reply With Quote
 
 
 
 
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'

>It dosnt work: only keep printing SuSE. Please help,
>
>Thanks in advance,
> -- /usr/bin/byte
>
>
>

 
Reply With Quote
 
 
 
 
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)
>
> 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
 
Reply With Quote
 
Byte
Guest
Posts: n/a
 
      02-12-2006
Great stuff, thanks:

-- /usr/bin/byte

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      02-12-2006
> 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'
 
Reply With Quote
 
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 <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie in XQUERY small question inarobis@gmail.com XML 0 07-20-2006 02:57 PM
Newbie in XQUERY small question inarobis@gmail.com XML 0 07-20-2006 02:38 PM
Small cameras getting too small? GRL Digital Photography 50 02-03-2006 03:12 AM
Small Square with small red X Peter Coddington Computer Support 4 01-03-2006 06:58 AM
NEWBIE QUESTION: Writing Small Program for Linux John Ryan C++ 3 11-02-2003 11:37 AM



Advertisments