Small newbie question

Discussion in 'Python' started by Byte, Feb 12, 2006.

  1. Byte

    Byte Guest

    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
    Byte, Feb 12, 2006
    #1
    1. Advertising

  2. Byte

    Astan Chee Guest

    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
    >
    >
    >
    Astan Chee, Feb 12, 2006
    #2
    1. Advertising

  3. Byte

    Tim Parkin Guest

    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
    Tim Parkin, Feb 12, 2006
    #3
  4. Byte

    Byte Guest

    Great stuff, thanks:

    -- /usr/bin/byte
    Byte, Feb 12, 2006
    #4
  5. Byte

    Paul Rubin Guest

    > 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'
    Paul Rubin, Feb 12, 2006
    #5
  6. On Sun, 12 Feb 2006 20:17:47 +0000, Tim Parkin <>
    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

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Feb 12, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. moonlight

    small question

    moonlight, Jan 15, 2006, in forum: VHDL
    Replies:
    1
    Views:
    444
  2. John Ryan
    Replies:
    3
    Views:
    375
    Troll_King
    Nov 2, 2003
  3. Replies:
    0
    Views:
    349
  4. Replies:
    0
    Views:
    347
  5. Jerry C.
    Replies:
    8
    Views:
    229
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page