Python program to create billing application for supermarket using functions
In this article, we will learn to create a billing application for supermarket using functions. This is a CBSE class 12 programming questions.
In the code that we are discussing here, we are using mainly 5 functions and a main function. Using this billing application the admin can add new items, view items, edit the existing details, and also search for a particular product. The customer can purchase any number of products according to their need. Finally, after completing the shopping an invoice is printed with the name of the products purchased, their quantities, price and total amount.
Given below is the code :
commodities = [] def view(): print('Total number of available commodities: ', len(commodities)) if len(commodities) != 0: print('---Commodities in stock---') for i in commodities: print() for key, value in i.items(): print(key, ' : ', value) def add(): print('\n---Add new items---\n') new = {} new['Name'] = input('Enter Item Name: ') while True: try: new['Quantity'] = int(input('Enter Item Quantity: ')) break except ValueError: print('Please enter quantity in digits only') while True: try: new['Price'] = int(input('Enter Price in Rupees: ')) break except ValueError: print('Please enter price in digits only') commodities.append(new) print('\n**New item has been successfully added**\n') def edit(): i_name = input('\nEnter name of the item to be edited: ') for i in commodities: if i_name.lower() == i['Name'].lower(): print('This is the current details of ' + i_name) print(i) print('\n!! Keep in mind that, only digits accepted as Quantity and Price\n') i['Name'] = input('Enter new name: ') i['Quantity'] = int(input('Enter new quantity: ')) i['Price'] = int(input('Enter new price: ')) print('\n--Item has been updated. Below is the updated details--') print(i) def search(): search = input('\nEnter item name to be searched: ') for i in commodities: if search.lower() == i['Name'].lower(): print('Item found. Details displayed below\n') print(i) def purchase(): cart = [] amount = 0 flag = 1 while flag: bag = {} purchase = input('\nEnter name of the item to be purchased: ') for i in commodities: if purchase.lower() == i['Name'].lower() : bag['name'] = purchase if i['Quantity'] != 0: quantity = int(input('Enter the quantity: ')) choice=input('Do you want to continue the purchase (y/n): ') if choice == 'y': bag['quantity'] = quantity bag['price'] = i['Price'] i['Quantity'] -= quantity amount += i['Price'] * quantity print('Item added to the cart.') cart.append(bag) else: break else: print('Sorry! Out of Stock') ch = input('\nDo you want to continue (y/n) : ') if ch == 'y': flag = 1 else: flag = 0 return amount, cart def main(): while True: print('\n--------------------SUPERMARKET BILLING APPLICATION--------------------') c = input('\n\t\t\t1. Admin\n\n\t\t\t2. Customer\n\n\t\tEnter choice : ') if c == '1': while True: print('\n---ADMIN MENU---') print('\n1. View Stock\n2. Add Item\n3. Edit Item\n4. Search \n5. Exit Admin menu\n') ch = input('Enter your choice : ') if ch == '1': view() elif ch == '2': add() elif ch == '3': edit() elif ch == '4': search() elif ch == '5': break else: print('Enter a valid option') elif c == '2': print('\n---CUSTOMER---\n') print('SNo.\tProduct\tPrice\n-----------------------') j = 1 for i in commodities: print('{}\t{}\t{}'.format(j, i['Name'], i['Price'])) j += 1 total, cart = purchase() print('\n\t============== INVOICE ==============\n') print('SNo.\tProduct\t\tQuantity\tPrice\n----------------------------------------------') j = 1 for i in cart: print('{}\t{}\t\t{}\t\t{}'.format(j, i['name'], i['quantity'], i['price'])) j += 1 print('----------------------------------------------') print('Total Amount:\t\t\t\t', total) print('\n$$$$ Thank You. Visit Again $$$$') else: print('Enter a valid option') if __name__ == '__main__': main()
Sample Outputs:
Subscribe
Login
Please login to comment
0 Discussion