Skip to content
Advertisement

how to print everything from a dictionary?

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)

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement