[Tutor] random.choice function
alan.gauld at btinternet.com
Sat Jan 19 12:56:16 CET 2008
"Cecilia Grahn" <cissi85 at gmail.com> wrote
> I am trying to make a script which returns a random string:
> ch = "So good to see you!","How are you?","Everything good
> today?","Glad you're here!".split(" ")
To python this line has 4 expressions which Pyhon will
assign as a tuple to ch. The first 3 expressions are literal
strings. The fourth is a string which is being split to
form 3 words contained in a list.
So ch winds up containing 3 strings and a list of 3 short
> everything works pretty much as I want it to, except when the last
> string is returned. It looks like this:
> Your name please:Cece
> Hello Cece ['Glad', "you're", 'here!']
So it returns a list of strings as you see here.
In other words you don't need the split operation.
> What is it I have missed or am I using random.choice wrong?
No, you are using split wrongly.
Author of the Learn to Program web site
More information about the Tutor