Thursday, December 26, 2024
Google search engine
HomeLanguagesCreating a Receipt Calculator using Python

Creating a Receipt Calculator using Python

A receipt calculator is generally a slip in which the total invoice along with their names is mentioned. We will use the class PrettyTable inside the prettytable library for making our receipt calculator.

What is PrettyTable ?

It is a class present inside prettytable library which help us to make relational tables in Python.  

Installation of library:

pip install prettytable

Generating PrettyTable :

Initialisation :
<table name> = PrettyTable(['<column1>','<column2>',....])

To add a row :
add_row(['<row1>','<row2>',....])

Approach :

There will be two columns: Item Name & Item Price. 

We will keep taking item name and item price (in new line) 
Until the user enters ‘q’ and store the price in another variable name ‘total’ initialized as 0. When user
Enters ‘q’, program will stop taking inputs and return the table along with the total amount specified at the end.

Below is the Implementation:

Python3




from prettytable import PrettyTable
  
  
print('--------------WELCOME TO XYZ Shop--------------\n')
table = PrettyTable(['Item Name', 'Item Price'])
total = 0
  
while(1):
    name = input('Enter Item name:')
      
    # 'q' to exit and print the table
    if(name != 'q'):
        price = int(input('Enter the Price:'))
          
        # store all the prices in 'total'
        total += price
        table.add_row([name, price])
        continue
      
    elif(name == 'q'):
        break
          
table.add_row(['TOTAL', total])
print(table)
print('\nThanks for shopping with us :)')
print('Your total bill amount is ', total, '/-')


Output:

Note: You can run this program on any python version except for python2 you just need to change the syntax of print().

RELATED ARTICLES

Most Popular

Recent Comments