generating random answers from a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • new2py
    New Member
    • Oct 2007
    • 4

    generating random answers from a list

    I am trying to write a script that allows input of raw question and returns a random answer from a list of 20 possibilities. Ihave the raw_input ok but the list is where I am having a problem. I have the list as tuple, dictionary and list. I can't assign a number variable to any of the list choices. Any help on how to parse strings in a list to print randomly?
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by new2py
    I am trying to write a script that allows input of raw question and returns a random answer from a list of 20 possibilities. Ihave the raw_input ok but the list is where I am having a problem. I have the list as tuple, dictionary and list. I can't assign a number variable to any of the list choices. Any help on how to parse strings in a list to print randomly?
    Like this?
    [code=python]
    import random

    myList = [1, 2, 3, 4, 5, 6 ,7, 8, 9,10]
    print random.choice(m yList)
    [/code]

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by new2py
      I am trying to write a script that allows input of raw question and returns a random answer from a list of 20 possibilities. Ihave the raw_input ok but the list is where I am having a problem. I have the list as tuple, dictionary and list. I can't assign a number variable to any of the list choices. Any help on how to parse strings in a list to print randomly?
      The random module has what you need.[code=Python]>>> import random
      >>> wordList = ['1', '2', '3', '4']
      >>> random.choice(w ordList)
      '1'
      >>> random.choice(w ordList)
      '3'
      >>> random.choice(w ordList)
      '4'
      >>> random.choice(w ordList)
      '3'
      >>> random.choice(w ordList)
      '1'
      >>> random.choice(w ordList)
      '2'
      >>> [/code]

      Comment

      • new2py
        New Member
        • Oct 2007
        • 4

        #4
        Originally posted by bvdet
        The random module has what you need.[code=Python]>>> import random
        >>> wordList = ['1', '2', '3', '4']
        >>> random.choice(w ordList)
        '1'
        >>> random.choice(w ordList)
        '3'
        >>> random.choice(w ordList)
        '4'
        >>> random.choice(w ordList)
        '3'
        >>> random.choice(w ordList)
        '1'
        >>> random.choice(w ordList)
        '2'
        >>> [/code]
        Will this only work with integers or can I assign a string to the integers?

        Comment

        • new2py
          New Member
          • Oct 2007
          • 4

          #5
          Originally posted by new2py
          Will this only work with integers or can I assign a string to the integers?

          Sorry answered my own question. Thanks for the help to both of you

          Comment

          Working...