[Tutor] user in put

Dave Kuhlman dkuhlman at rexx.com
Mon Aug 27 22:32:24 CEST 2007


On Mon, Aug 27, 2007 at 11:02:59AM -0700, Latasha Marks wrote:

> Need help get a user to in put his or her favortie food the the
> program should the n print oue the name of the new food by
> joining the original food names together

If what you want is to enable your user to enter several foods
(strings), then concatenate them together, try something like the
following:

    In [33]: foods = []
    In [34]: food = raw_input('What is your favorite food?')
    What is your favorite food?peaches
    In [35]: foods.append(food)
    In [36]: food = raw_input('What is your favorite food?')
    What is your favorite food?nectarines
    In [37]: foods.append(food)
    In [38]: food = raw_input('What is your favorite food?')
    What is your favorite food?cantaloupe
    In [39]: foods.append(food)
    In [40]: ', '.join(foods)
    Out[40]: 'peaches, nectarines, cantaloupe'

Note that we append each food to a list, then do string.join(). 
That's faster than doing multiple string concatenations.  In this
case there are not enough strings to make a difference.  But, it's
a good habit to get into.

Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list