1

Given three input dictionaries, we want to output a table with one row per dictionary entry. The width of each column should be exactly five characters longer than the longest item in that column. I could write a code that computes the width of the column, yet individually for each dictionary. Is there a way to make it automatic by using it within the str.format() method? These are the 3 dictionaries:

data = {"Charmander": {"type": "Fire", "first_evolution": "Charmeleon", "second_evolution": "Charizard"}, "Bulbasaur": {"type": "Grass/Poison", "first_evolution": "Ivysaur", "second_evolution": "Venusaur"}, "Squirtle": {"type": "Water", "first_evolution": "Wartortle", "second_evolution": "Blastoise"}}

data = {"Cyndaquil": {"type": "Fire", "first_evolution": "Quilava", "second_evolution": "Typhlosion"}, "Totodile": {"type": "Water", "first_evolution": "Croconaw", "second_evolution": "Feraligatr"}, "Chikorita": {"type": "Grass", "first_evolution": "Bayleef", "second_evolution": "Meganium"}}

data = {"Treecko": {"type": "Grass", "first_evolution": "Grovyle", "second_evolution": "Sceptile"}, "Mudkip": {"type": "Water", "first_evolution": "Marshtomp", "second_evolution": "Swampert"}, "Torchic": {"type": "Fire", "first_evolution": "Combusken", "second_evolution": "Blaziken"}}

My code is the following:

width = max(len(x) for x in data) + 5
sorted_data = sorted(data.items())
sorted_dictionary = dict(sorted_data)
for pok, char in sorted_dictionary.items():
    print("{0:width} {1:width} {2:width} {3:width}".format(pok, char["type"], char["first_evolution"], char["second_evolution"]))

When I run the code, I get the following error message:

ValueError: Invalid format specifier

Barmar
  • 741,623
  • 53
  • 500
  • 612
Kay
  • 19
  • 5

0 Answers0