How to print random strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • theboringdays@gmail.com

    #1

    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.randrang e() 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?

  • Jorge Godoy

    #2
    Re: How to print random strings

    theboringdays@g mail.com writes:
    [color=blue]
    > 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.randrang e() 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?[/color]

    How about using the integer as an index to access the elements of a list? ;-)

    --
    Jorge Godoy <godoy@ieee.org >

    Comment

    • Mike Meyer

      #3
      Re: How to print random strings

      theboringdays@g mail.com writes:
      [color=blue]
      > 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.randrang e() 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?[/color]

      Well, randrange can be used to do this, but random.choice is more
      pythonic.

      <mike
      --
      Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

      Comment

      • Magnus Lycka

        #4
        Re: How to print random strings

        theboringdays@g mail.com wrote:[color=blue]
        > So how would I go about have 5 strings, and running a program that will
        > randomly pick one of those to print?[/color]
        [color=blue][color=green][color=darkred]
        >>> import random
        >>> text = "Perhaps reading the manual is a good idea?".split()
        >>> random.choice(t ext)[/color][/color][/color]
        'is'[color=blue][color=green][color=darkred]
        >>> random.choice(t ext)[/color][/color][/color]
        'reading'[color=blue][color=green][color=darkred]
        >>> random.choice(t ext)[/color][/color][/color]
        'a'[color=blue][color=green][color=darkred]
        >>> random.choice(t ext)[/color][/color][/color]
        'the'[color=blue][color=green][color=darkred]
        >>> random.choice(t ext)[/color][/color][/color]
        'Perhaps'

        See http://docs.python.org/lib/module-random.html

        Comment

        • nak

          #5
          Re: How to print random strings

          theboringdays@g mail.com wrote:[color=blue]
          > 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.randrang e() 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?[/color]

          The fortune cookie program can be made by copying the code for 'Mood
          Computer' on page 64 and 65. So it could be something like:

          import random
          print "Welcome to the Fortune cookie program"
          fortune = random.randrang e(3)
          if fortune == 0:
          print "some fortune1"
          elif fortune == 1:
          print "some fortune2"
          elif fortune == 2:
          print "some fortune3"

          Comment

          Working...