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:
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.