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?
4 5731
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? 
import random


myList = [1, 2, 3, 4, 5, 6 ,7, 8, 9,10]

print random.choice(myList)

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.  >>> import random

>>> wordList = ['1', '2', '3', '4']

>>> random.choice(wordList)

'1'

>>> random.choice(wordList)

'3'

>>> random.choice(wordList)

'4'

>>> random.choice(wordList)

'3'

>>> random.choice(wordList)

'1'

>>> random.choice(wordList)

'2'

>>>
The random module has what you need.  >>> import random

>>> wordList = ['1', '2', '3', '4']

>>> random.choice(wordList)

'1'

>>> random.choice(wordList)

'3'

>>> random.choice(wordList)

'4'

>>> random.choice(wordList)

'3'

>>> random.choice(wordList)

'1'

>>> random.choice(wordList)

'2'

>>>
Will this only work with integers or can I assign a string to the integers?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...

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.
...

by: Leon 
last post by:
I need a program that generate 5 nonduplicates random number between 110
as string values store in an array.
Do anybody know of any good books or websites that explain how to generator
random...

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...

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...

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...

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.
...

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...

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());...

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...

by: jianzs 
last post by:
Introduction
Cloudnative applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...

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...

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...

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...

by: stefan129 
last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multidomain SSL certificates? Any recommendations on reliable providers or specific...

by: egorbl4 
last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это
Что это? Что мне с этим делать?
...

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...

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 highfrequency records to 61 million...
 