0

I have this code that prints a invoice for school but instead of doing it the way I did i need to use string formatting.

if(quantity==1):
    item1=cart[0].split("^")
    item1price = (item_price[item_number.index(item1[1])]*int(item1[0]))
    item1rounded = item1price.round(item1price,2)
    print("Order Date:     {}/{}/{}".format(month,day,year))
    print("  Customer:          {}        {}".format(cust_code,customer_name[customer_number.index(int(cust_code))]))
    print("")
    print("Ln#   Item #            Item Description             Req Date         Qty      Price         Total")
    
    print(" 1    {}             {}         {}/{}/{}        {}      {}   $   {:.2f}".format(item1[1],item_description[item_number.index(item1[1])],item1[2][:2],item1[2][2:4],item1[2][4:],item1[0],item_price[item_number.index(item1[1])],(item_price[item_number.index(item1[1])]*int(item1[0]))))
    print("")

    print("                                                                           Total           {:.2f}".format((item_price[item_number.index(item1[1])]*int(item1[0]))))
    
     
if(quantity==2):
    item1=cart[0].split("^")
    item2=cart[1].split("^")
    item1price = (item_price[item_number.index(item1[1])]*int(item1[0]))
    item1rounded = item1price.round(item1price,2)
    
    
    print("Order Date:     {}/{}/{}".format(month,day,year))
    print(  "  Customer:          {}           {}".format(cust_code,customer_name[customer_number.index(int(cust_code))]))
    print ("")
    print("Ln#   Item #            Item Description             Req Date         Qty      Price         Total")
    
    print(" 1    {}             {}                     {}/{}/{}        {}       {}   $    {}".format(item1[1],item_description[item_number.index(item1[1])],item1[2][:2],item1[2][2:4],item1[2][4:],item1[0],item_price[item_number.index(item1[1])],(item_price[item_number.index(item1[1])]*int(item1[0]))))
   
    print(" 2    {}              {}              {}/{}/{}      {}      {}   ${:.2f}".format(item2[1],item_description[item_number.index(item2[1])],item2[2][:2],item2[2][2:4],item2[2][4:],item2[0],item_price[item_number.index(item2[1])],(round(item_price[item_number.index(item2[1])]*float(item2[0]),2))))
    print("") 
    print("                                                                           Total        {:.2f}".format((item_price[item_number.index(item2[1])]*int(item2[0]))+(item_price[item_number.index(item1[1])]*int(item1[0]))))

can someone help me string format this it needs to be something like this

enter image description here

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I don't understand the question. You're using the `str.format()` method, isn't that string formatting? – Barmar Nov 06 '20 at 01:18
  • 3
    Does this answer your question? [How do I make a fixed size formatted string in python?](https://stackoverflow.com/questions/10837017/how-do-i-make-a-fixed-size-formatted-string-in-python) – Serial Lazer Nov 06 '20 at 01:19
  • sorry yeah im not the greatest at explaining instead of using the spaces we need to do something like [>:10}{>:10} im just really confused what to put in each brace – Joe Castaldo Nov 06 '20 at 01:30

1 Answers1

0

actually you need to iterate the cart list and print each line. eg:

cart = [
  [20, 30],
  [40, 80]
]

# print the header before the loop
for (ln, item) in enumerate(cart,1):
    print(" {} {} {}".format(ln, item[0], item[1])) 

try the snippet above and you will figure out what you need to do and check the enumerate documentation.

regards.

Fausto Alonso
  • 1,046
  • 6
  • 8