Script prints out of order

I am using Python 2.7.3 and am trying to understand why this script is executing print instructions out of order. that is, "-" prints AFTER the 2nd cycle of the cycle.

My script:

def cheeseshop(kind, *arguments, **keywords):
    print "-- Do you have any", kind, "?"
    print "-- I'm sorry, we're all out of", kind
    for arg in arguments:
        print arg
    print "-" * 40
    keys = sorted(keywords.keys())
    for kw in keys:
        print kw, ":", keywords[kw]

cheeseshop("Limburger", "It very runny, sir.",
           "It really very, VERY runny, sir.",
           {'shopkeeper':'Michael Palin',
           'client':"John Cleese",
           'sketch':"Cheese Shop Sketch"})

      

Output:

-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It very runny, sir.
It really very, VERY runny, sir.
{'shopkeeper': 'Michael Palin', 'sketch': 'Cheese Shop Sketch', 'client': 'John Cleese'}
----------------------------------------

      

Why is print "-" * 40 executed BEFORE the dictionary, as you would expect?

+3


source to share


1 answer


You have not passed the dictionary as keywords. Use the syntax **

to do this:

cheeseshop("Limburger", "It very runny, sir.",
           "It really very, VERY runny, sir.",
           **{'shopkeeper':'Michael Palin',
           'client':"John Cleese",
           'sketch':"Cheese Shop Sketch"})

      



or not using a dictionary at all:

cheeseshop("Limburger", "It very runny, sir.",
           "It really very, VERY runny, sir.",
           shopkeeper='Michael Palin',
           client="John Cleese",
           sketch="Cheese Shop Sketch")

      

+8


source







All Articles