[Tutor] Hi Tutor

Martin A. Brown martin at linux-ip.net
Sat Jan 9 16:56:47 EST 2016


>I wrote this short program for my grandson:
>
>from random import sample
>
>soups = ['Onion soup', 'Veggie soup', 'Chicken soup', 'Corn soup']
>salads = ['Veggie', 'Onion', 'Cabbage', 'Lettuce', 'Caesar', 'Tomato']
>main = ['Crab cake', 'Catfish', 'Ribs', 'Chopped liver', 'Meat balls']
>beverage = ['Wine', 'Rum', 'Lemonade', 'Red bull', 'Margarita', 'Jin']
>
>def dish(soups):
>    return (sample(soups, 1))
>
>print('Soup:\t\t', dish(soups))
>print('Salad:\t\t', dish(salads))
>print('Main dish:\t', dish(main))
>print('Beverage:\t', dish(beverage))
>
>A possible output could be:
>
>Soup: ['Chicken soup']
>Salad: ['Caesar']
>Main dish: ['Meat balls']
>Beverage: ['Wine']
>
>How do I get rid from the square brackets and the quotation marks 
>in the output?

There are many possible answers to this question.  Here's my answer:

  from random import choice

  def dish(options):
      return choice(options)

Then, the function dish() will return exactly one element from the 
options.  Since each of soup, salads, main and beverage are lists 
with string elements, the dish() function will return a string.

I would like to have some Onion soup, the Crab cake, Rum and a 
Caesar, please.

Good luck,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/


More information about the Tutor mailing list