Python Forum
Improve this code (Receipt alike)
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Improve this code (Receipt alike)
#11
def print_heading(): 
    print('{:^50s}'.format("MyShop"))
    print('Prodcode        Quantity        Price   Amount(RM)\n')
 
def print_receipt(entry): 
    total = float(entry[1] * entry[2]) 
    print('{:<16}{:<16}{:<8.2f}{:<8.2f}'.format(entry[0], entry[1], entry[2], total))
    return total
 
def my_purchases(): 
    prodcodes = ['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF'] 
    prices = [5.00, 7.00, 2.30, 1.70, 9.00, 10.50] 
    
    prods = [] 
    qty = 1    
 
    while True: 
        prod = input('Product code (x to exit): ')
        prod = prod.upper()
        if prod == '' or prod == 'X':
            break
        elif prod in prodcodes:
            price = prices[prodcodes.index(prod.strip())] 
            prods.append([prod, qty, price]) 
        else:
            print('Invalid product code, try again')
            continue
        qty = (input("Enter Quantity: "))
        if qty == '' or qty == '0':
            break
        qty = int(qty)
        
 
    print_heading()
    total = 0
    for entry in prods:
        total += print_receipt(entry)
    print('Total: ${:<8.2f}'.format(total))



if __name__ == '__main__':  #If __name__ == '__main__' then can run the next line.Unless there is imported python file at the beginning.
    my_purchases()
Output:
Product code (x to exit): BBB Enter Quantity: 1 Product code (x to exit): DDD Enter Quantity: 2 Product code (x to exit): EEE Enter Quantity: 4 Product code (x to exit): x MyShop Prodcode Quantity Price Amount(RM) BBB 1 7.00 7.00 DDD 1 1.70 1.70 EEE 2 9.00 18.00 Total: $26.70
Why those it not store properly...?
I changed the arrangement of the Prod and Quantity only...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I improve string similarity in my current code? SUGSKY 3 2,366 May-28-2020, 05:16 AM
Last Post: deanhystad
  How will you improve my code? Gateux 4 2,385 Jul-20-2019, 12:55 PM
Last Post: ndc85430
  Help!! Order form and receipt code beginner1996 2 3,287 Mar-07-2019, 03:22 PM
Last Post: beginner1996
  Python Coding Homework Help "Grocery Store Receipt" cd3 3 12,632 Apr-12-2017, 02:10 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020