Jun-26-2018, 03:33 PM
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...