Python Forum

Full Version: Why does my code not execute? (def function with 'for in' iteration over a dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi^^

I have troubles to find my mistake within following code:
def printPicnic(itemsDict, leftWidth, rightWidth):
    print('PICNIC ITEMS'.center(leftWidth + rightWidth, '-'))
    for k, v in itemsDict.items():
        print(k.ljust(leftWidth, '.') + str(v).rjust(rightWidth))
        picnicItems = {'sandwiches': 4, 'apples': 12, 'cups': 4, 'cookies':8000}
        printPicnic(picnicItems, 12, 5)
        printPicnic(picnicItems, 20, 6)
I have checked the code line by line several times and compared to the description my my study book, but could not spot my mistake.
It just would not execute.

Anyone could help out and hint me where I got it wrong?

Regards,
Placebo
Last 3 lines should be outside the function, i.e. no indentation at all
(Oct-18-2018, 06:07 AM)Placebo Wrote: [ -> ]It just would not execute.
In fact this code does execute very well, but you don't see anything because all it does is define a function named printPicnic and this function is never called. As Buran said you need to unindent the last three lines to obtain the intended effect.
Omg, Im obviously still such a big noob xD

Thanks a lot for clearing it up:)