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?
generating random answers from a list
Collapse
X
-
Originally posted by new2pyI 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?
[code=python]
import random
myList = [1, 2, 3, 4, 5, 6 ,7, 8, 9,10]
print random.choice(m yList)
[/code] -
Originally posted by new2pyI 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?
>>> 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
-
Originally posted by bvdetThe 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
Comment