471,338 Members | 1,009 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

generating random answers from a list

4
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?
Oct 26 '07 #1
4 5138
ilikepython
844 Expert 512MB
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?
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. myList = [1, 2, 3, 4, 5, 6 ,7, 8, 9,10]
  4. print random.choice(myList)
  5.  
Oct 27 '07 #2
bvdet
2,851 Expert Mod 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> wordList = ['1', '2', '3', '4']
  3. >>> random.choice(wordList)
  4. '1'
  5. >>> random.choice(wordList)
  6. '3'
  7. >>> random.choice(wordList)
  8. '4'
  9. >>> random.choice(wordList)
  10. '3'
  11. >>> random.choice(wordList)
  12. '1'
  13. >>> random.choice(wordList)
  14. '2'
  15. >>> 
Oct 27 '07 #3
new2py
4
The random module has what you need.
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> wordList = ['1', '2', '3', '4']
  3. >>> random.choice(wordList)
  4. '1'
  5. >>> random.choice(wordList)
  6. '3'
  7. >>> random.choice(wordList)
  8. '4'
  9. >>> random.choice(wordList)
  10. '3'
  11. >>> random.choice(wordList)
  12. '1'
  13. >>> random.choice(wordList)
  14. '2'
  15. >>> 
Will this only work with integers or can I assign a string to the integers?
Oct 27 '07 #4
new2py
4
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
Oct 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Ross MacGregor | last post: by
6 posts views Thread by Leon | last post: by
2 posts views Thread by Simon Wittber | last post: by
15 posts views Thread by caca | last post: by
26 posts views Thread by bilgekhan | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.