473,236 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 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 5731
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

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

Similar topics

5
by: Ross MacGregor | last post by:
I have a very simple yet complicated problem. I want to generate a random list of indices (int's) for a container. Let's say I have a container with 10 items and I want a list of 3 random...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
6
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
2
by: Simon Wittber | last post by:
I'm building a web application using sqlalchemy in my db layer. Some of the tables require single integer primary keys which might be exposed in some parts of the web interface. If users can...
2
by: randomcz | last post by:
hi, i need help with generating random numbers; The task is to generate hundreds of random vectors, like 1,3,5,6,7 2,4,5,4,8 ... I used the current time as the random seed, but it turns...
5
by: dav3 | last post by:
I have almost completed a monster assignment on sorting algorithms (quick, insertion and selection) using c++ but I am lost on one part of the assignment. I have to generate a random list of numbers...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
15
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.