1

I'm trying to print a menu from information within a file, so I took the information and created a dictionary. I'm trying to figure out if there is a way to format the spacing between values, so I can align the values with headers. I am also trying to find out if there is anyway that each key and its values will print without a line of space in the middle. My code currently looks like this:

#import classes
import inventory


#define constant for file
INVENTORY = "inventory.txt"

#define main
def main():
    #create empty dictionary to fill later
    inventory_dict = {}
    #call function to process inventory
    process_inventory(inventory_dict)
    #call function to print menu
    menu(inventory_dict)


#define process file inventory function
def process_inventory(inventory_dict):

    #open inventory file
    inventory_file = open(INVENTORY, 'r')

    #create inventory instances
    for item in inventory_file:

        #split line to reash
        inventory_list = item.split(",")

        #create variables to store each quality
        item_id = inventory_list[0]
        item_name = inventory_list[1]
        item_qty = inventory_list[2]
        item_price = inventory_list[3]

        #create object
        product = inventory.Inventory(item_id,item_name,item_qty,item_price)

        #add qualities to inventory dictionary
        inventory_dict[item_id] = product

    #close file
    inventory_file.close()

#define function to print menu
def menu(inventory_dict):
    #print headers
    print("ID Item Price Qty Avaliable")

    #print object information
    for object_id in inventory_dict:
        print(inventory_dict[object_id])

    #print how to exit
    print("Enter 0 when finished")

    #print blank line
    print(" ")

When printing the dictionary the output looks like:

Output

Why did it print the first two keys with values line after line with no space but others with a line in between? I want to align each value with it's header. Is that possible? If so how? The only way I could think to do so would be with the print function and to format it myself.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
Maddi
  • 27
  • 2

1 Answers1

0

The reason they don't align is because each item is a different length. The first item in each row pushes the next items farther to the right the longer it is.

This is a modified version of your menu function that fixes this:

def menu(inventory_dict):
    maxLength = 0
    for row in inventory_dict.values():
        for item in row.values():
            maxLength = max(len(item), maxLength) # If the item is longer than our current longest, make it the longest
    #print headers
    for header in ("ID", "Item", "Price", "Qty", "Avaliable"):
        print(header.ljust(maxLength)) # Fill spaces in until the header is as long as our longest item
    print()

    #print object information
    for row in inventory_dict.values():
        for item in row.values():
            print(item.ljust(maxLength)) # Fill spaces in until the header is as long as our longest item

    #print how to exit
    print("Enter 0 when finished")

    #print blank line
    print()

I can't verify if it works because I don't have your inventory module though.

timotree
  • 1,325
  • 9
  • 29