473,287 Members | 1,564 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,287 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 5736
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());...
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.