[Tutor] random.choice function

Remco Gerlich remco at gerlich.nl
Sat Jan 19 13:02:48 CET 2008


Hi,

This line:

ch = "So good to see you!","How are you?","Everything good
today?","Glad you're here!".split(" ")

Creates a tuple with 4 elements:

1. "So good to see you!"
2. "How are you?"
3. "Everything good today?"
and
4. "Glad you're here!".split(" "), which is equal to ["Glad","you're",
"here"].

So it would seem that you just need to remove the .split(" "). It's probably
there from a previous try? What it does is break the string into a list,
splitting at every space.

Also, I'd recommend adding ( and ) around the tuple for readability, so
you'd get this:
ch = ("So good to see you!",
      "How are you?",
      "Everything good today?",
      "Glad you're here!",
)

Also a last ',' so it's easier to add new lines, move them around, delete
lines etc without accidently forgetting the comma. But these things are just
a matter of taste.

Remco


On Jan 19, 2008 12:37 PM, Cecilia Grahn <cissi85 at gmail.com> wrote:

> Hi,
> this is my first mail and I apologize if I got something wrong =)
> I am trying to make a script which returns a random string:
>
> #hello.py
> # Get the user's name and print a friendly hello
> import random, string
> name = raw_input("Your name please:")
> ch = "So good to see you!","How are you?","Everything good
> today?","Glad you're here!".split(" ")
> x = random.choice(ch)
> print "Hello", name, x
>
> everything works pretty much as I want it to, except when the last
> string is returned. It looks like this:
>
>  >>> ================================ RESTART
> ================================
>  >>>
> Your name please:Cece
> Hello Cece ['Glad', "you're", 'here!']
>  >>>
>
> What is it I have missed or am I using  random.choice wrong?
>
>
> /CeCe
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080119/70210311/attachment.htm 


More information about the Tutor mailing list