#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    27
    Rep Power
    0

    How to print random strings


    Im at the end of chapter 3 of "Python Programming For The Absolute Beginner, Michael Dawson " and he asks to make a fortune program that displays a fortune each time its ran, and to have 5 unique fortunes.

    Whats confusing is that, he never discussed how to do this. The only thing he talked about was using random.randrange() and I tried that with text but it seems like its only for integers as it complains when I put text in the argument.

    So how would I go about have 5 strings, and running a program that will randomly pick one of those to print?

    I think he may have forgot to cover something?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Make a list that contains 5 strings.
    Import the random library.
    Use the random module's choice function.
    To use it, check out the following:
    Code:
    from random import *
    fortunes = ['text', 'text2', 'text3', 'text4', 'text5']
    print choice(fortunes)
    This will print out a random element of the fortunes list.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    27
    Rep Power
    0
    yea what the heck, the author didnt cover the list or the "choice" part. How dooes he expect us to do that challenge with only teaching random.randrange() ? Thanks, im going to email this dude.
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I didn't get to read the tutorial myself. It's feasible that you may have perhaps missed something? Also, the method I showed you is just one way to do this, in my opinion it is the easiest and most effective way. However there are other ways.
  8. #5
  9. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    This will work with random.randrange().
    Code:
    #!/usr/bin/python
    
    import random
    
    fortunes = ('first', 'second', 'third', 'fourth', 'fifth')
    
    number = random.randrange(0, 5)
    
    print fortunes[number]
  10. #6
  11. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    python Code:
    #!/usr/bin/env python
    from os import system
    # Not reinventing the wheel == priceless <img src="http://images.devshed.com/fds/smilies/smile.gif" border="0" alt="" title="Smilie" class="inlineimg" />
    system( "/usr/bin/env fortune" )


    --Simon

    Comments on this post

    • mvantuyl agrees : Truly!
    • CyBerHigh agrees : Good Point!
    • Peyton agrees : Haha
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65

    Talking


    If tuples and lists have not been covered yet, then use something like this:
    Code:
    import random
    
    number = random.randrange(1, 6)
    
    if number == 1:
        print "fortune1"
    if number == 2:
        print "fortune2"
    if number == 3:
        print "fortune3"
    if number == 4:
        print "fortune4"
    if number == 5:
        print "fortune5"

IMN logo majestic logo threadwatch logo seochat tools logo