[Tutor] newbie question

Alan Gauld alan.gauld at btinternet.com
Mon Oct 15 18:47:56 CEST 2007


"Ramkumar Kashyap" <ramkumar.kashyap at gmail.com> wrote

>>>> menu_specials = {"breakfast" : "sausage and eggs",
> ... "lunch" : "split pea soup and garlic bread",
> ... "dinner": "2 hot dogs and onion rings"}

>>>> print "%s" % menu_specials["breakfast"]

You don't really need the %s bit it could just be

>>> print menu_specials["breakfast"]

> I am trying to print out the entire dictionary but am getting an 
> error.

>>> print "%s %s %s" % menu_specials["breakfast", "lunch", "dinner"]

The bit between [] is the key to the dictionary(notice key is 
singular!)
You have passed a tuple of 3 strings (a tuple because they are
separated by commas) as a key. But the dictionary keys are all
single strings not tuples. In other words you need to access the
dictionary 3 times like so:

>>> print "%s %s %s" % menu_specials["breakfast"], 
>>> menu_specials["lunch"], menu_specials["dinner"]

> print "%s %s %s" % menu_specials["breakfast"], 
> menu_specials["lunch"],
> menu_specials["dinner"]

That should have worked. are you sure you had it all on one line?
If you need to break the line use a \ character.
My previous example could be done like this:

>>> print "%s %s %s" % menu_specials["breakfast"], \
                                      menu_specials["lunch"], \
                                      menu_specials["dinner"]

Or missing the formatting string as:

>>> print menu_specials["breakfast"], \
              menu_specials["lunch"], \
              menu_specials["dinner"]

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list