I'm trying to do this question for class that is really confusing me, I can't seem to figure it out. They give me this code and say that I need to do something to make it print all of the flowers in the dictionary instead of printing just one. Here is the code that is provided, which just prints one of the flowers.
def build_floralArrangement(size, **flowers): # **flowers is a list of key-value pairs to add to the dictionary.
"""Build a dictionary containing customer requests for flowers."""
arrangement = {}
arrangement['size'] = size
for key, value in flowers.items():
arrangement[key] = value
return arrangement
myArrangement = build_floralArrangement('large', flower1 = 'tulips', flower2='red roses', flower3='white carnations')
print(myArrangement)
CodePudding user response:
This should work:
def build_floralArrangement(size, **flowers): # **flowers is a list of key-value pairs to add to the dictionary.
"""Build a dictionary containing customer requests for flowers."""
arrangement = {}
arrangement['size'] = size
for key, value in flowers.items():
arrangement[key] = value
return arrangement
myArrangement = build_floralArrangement('large', flower1 = 'tulips', flower2='red roses', flower3='white carnations')
print(myArrangement)
Output:
{'size': 'large', 'flower1': 'tulips', 'flower2': 'red roses', 'flower3': 'white carnations'}
All I did was move your return
statement out of your for
loop.
CodePudding user response:
unindent return arrangement
because this way you are returning a single key-value object:
for key, value in flowers.items():
arrangement[key] = value
return arrangement
If you want to show only the flowers I would do the following (as advice):
myArrangement = build_floralArrangement('large', flower = 'tulips', flower='red roses', flower='white carnations')
and then:
for key, value in flowers.items():
arrangement[key] = value
return arrangement
And then to show the flowers:
print(arrangement.get('flowers'))
In this way you have a simpler dictionary and that can be used for more things, I hope it helps you