I'm confused with the statement " print(kw,":",keywords[kw])" in the following program, in python.
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)
print(keywords)
keys=sorted(keywords)
print(keys)
for kw in keys:
print(kw,":",keywords[kw])
cheeseshop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
shopkeeper="Michael Palin",
client="John Cleese",
sketch="Cheese Shop Sketch")
The result is below:
--Do you have any Limburger ?
--I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
{'client': 'John Cleese', 'sketch': 'Cheese Shop Sketch', 'shopkeeper': 'Michael Palin'}
['client', 'shopkeeper', 'sketch']
client : John Cleese
shopkeeper : Michael Palin
sketch : Cheese Shop Sketch
In my idea, "kw" should be 'client', 'sketch' and 'shopkeeper', not numbers, then how can "kw" be the index of keywords in the statement " print(kw,":",keywords[kw])"?
To verify my idea, I also tried another program:
letters=['a','b']
for kw in letters:
print(letters[kw])
And a reasonable reply pops up:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: list indices must be integers, not str
That furthermore confuses me for the problem I got in the first piece of program.I think it should pop up the same error to me.