Item Price Generator Hackerrank Solution

Introduction

In this tutorial, we will provide an item price generator hackerrank solution to split a bill evenly. I’ll guide you through the process step by step, but my whole code is available at the conclusion of the article. Also, this is a project not a direct hackerrank question solution, for the hackerrank solution, we have created a separate function, you can copy and paste that complete function for your solution. Click here to get Item Price Generator hackerrank solution.

Step-by-step solution for Item Price Generator:

Step.1 – Create a Python file and add libraries

# Importing all the necessary modules
from tkinter import * 

The project is started by importing all of the required libraries and modules. The graphics will be created using the Tkinter package. The Tkinter window must first be created, and the required libraries must then be imported.

Step.2 – Creating a Window For GUI!

# Setting the main screen 
root = Tk() 
root.title("CopyAssignment.com")
root.geometry("360x300")
root.resizable(width=False,height=False)

After you’ve imported all of the modules, it’s time to launch Tkinter and construct the program’s graphical user interface. To begin, we’ll start Tkinter and create a window for our main goal. We set the window size to 360×300 and the window resizability to false in this scenario.

Step.3 – Creating a Bill Split and Tip Counter Function!

def billsplit():
    # Getting the number of people
    people=peoples.get()
    # Getting the total bill amount
    bill=total_amount.get()
    # Getting the tip percentage
    tip=tips.get()
    # Getting the tax percentage
    tax=taxs.get()
    # Calculating the tip amount
    tip_amount=(int(tip)/100)*bill
    # Calculating the tax amount
    tax_amount=(int(tax)/100)*bill
    # Calculating the total bill amount
    total_bill=bill+tip_amount+tax_amount
    # Calculating the amount each person has to pay
    amount=total_bill/int(people)
    # Printing the amount each person has to pay
    Label(root, font = ('arial', 13, 'bold',),
    text=f"Each person has to pay Rs.{amount}",fg="Red").grid(row=7,columnspan=4)

We’ll need to prompt the user for the amount of the bill, the % tip they wish to provide, and how many people are sharing the cost in order to determine the tip and what each person pays for the bill.

We can determine how much each individual must pay now that we know all of the information about the bill and how many people are dividing it. Finally, publish the amount that each individual must pay.

We’ll also include the amount with the tip so the user doesn’t have to perform the arithmetic. The total bill and tip amount must be converted to float values.

Step.4 – Creating Input Fields, Labels, and Submit Button

# Creating and placing all the components of the window
total_amount=DoubleVar()
peoples=DoubleVar()
tips=DoubleVar()
taxs=DoubleVar()

Label(root, font = ('arial', 16, 'bold','underline'),
text="Split Your Bill Here!\n").grid(row=1,columnspan=4)

####### total Bill Mount ########
Label(root, font = ('arial', 11, 'bold'),
text="Enter Total Amount: ").grid(row=3,column=1)

t_amount=Entry(root,textvariable=total_amount,width=5,
font =('arial', 15, 'bold')).grid(row=3,column=2)

############ How many people ############
Label(root, font = ('arial', 11, 'bold'),
text="How many people to split the bill?").grid(row=4,column=1)

no_of_people=Entry(root,textvariable=peoples,width=5,
font = ('arial', 15, 'bold')).grid(row=4,column=2)

############ Enter % of Tips ############
Label(root, font = ('arial', 11, 'bold'),
text="How much % Tip would you like to give?").grid(row=5,column=1)

Tip_Amount=Entry(root,textvariable=tips,
width=5,font = ('arial', 15, 'bold')).grid(row=5,column=2)

############ Enter % of Taxs ############
Label(root, font = ('arial', 11, 'bold'),
text="How much Tax % ?").grid(row=6,column=1)

secbTax_Amounttn=Entry(root,textvariable=taxs,
width=5,font = ('arial', 15, 'bold')).grid(row=6,column=2)


###########################Footer############################################
Label(root, font = ('arial', 10, 'bold'),
text="\n\nWelcome At The CopyAssignment.com").grid(row=8,columnspan=6)

#Submit Button
setbtn=Button(root,text="Split your Bill!",command=billsplit,bg="#D4AC0D",
fg="Black",font = ('arial', 19, 'bold')).grid(row=9,columnspan=4)

mainloop() 

Now that the GUI has been constructed, you can start adding Input Fields, labels, and buttons to it. To begin, we’ll create two simple labels. Labels such as “Split Your Bill Here” and others will be shown in the window to notify consumers about the program and how to interact with it. Then, we’ll make an input field that employs a Doublevar() to store all of the double values that users may enter.

Complete Code of Item Price Generator in Python

# Importing all the necessary modules
from tkinter import * 

# Setting the main screen 
root = Tk() 
root.title("CopyAssignment.com")
root.geometry("360x300")
root.resizable(width=False,height=False)

######Creating a Bill Split and Tip Counter Function!#########

def billsplit():
    # Getting the number of people
    people=peoples.get()
    # Getting the total bill amount
    bill=total_amount.get()
    # Getting the tip percentage
    tip=tips.get()
    # Getting the tax percentage
    tax=taxs.get()
    # Calculating the tip amount
    tip_amount=(int(tip)/100)*bill
    # Calculating the tax amount
    tax_amount=(int(tax)/100)*bill
    # Calculating the total bill amount
    total_bill=bill+tip_amount+tax_amount
    # Calculating the amount each person has to pay
    amount=total_bill/int(people)
    # Printing the amount each person has to pay
    Label(root, font = ('arial', 13, 'bold',),
    text=f"Each person has to pay Rs.{amount}",fg="Red").grid(row=7,columnspan=4)
    
# Creating and placing all the components of the window
total_amount=DoubleVar()
peoples=DoubleVar()
tips=DoubleVar()
taxs=DoubleVar()

Label(root, font = ('arial', 16, 'bold','underline'),
text="Split Your Bill Here!\n").grid(row=1,columnspan=4)

####### total Bill Mount ########
Label(root, font = ('arial', 11, 'bold'),
text="Enter Total Amount: ").grid(row=3,column=1)

t_amount=Entry(root,textvariable=total_amount,width=5,
font =('arial', 15, 'bold')).grid(row=3,column=2)

############ How many people ############
Label(root, font = ('arial', 11, 'bold'),
text="How many people to split the bill?").grid(row=4,column=1)

no_of_people=Entry(root,textvariable=peoples,width=5,
font = ('arial', 15, 'bold')).grid(row=4,column=2)

############ Enter % of Tips ############
Label(root, font = ('arial', 11, 'bold'),
text="How much % Tip would you like to give?").grid(row=5,column=1)

Tip_Amount=Entry(root,textvariable=tips,
width=5,font = ('arial', 15, 'bold')).grid(row=5,column=2)

############ Enter % of Taxs ############
Label(root, font = ('arial', 11, 'bold'),
text="How much Tax % ?").grid(row=6,column=1)

secbTax_Amounttn=Entry(root,textvariable=taxs,
width=5,font = ('arial', 15, 'bold')).grid(row=6,column=2)


###########################Footer############################################
Label(root, font = ('arial', 10, 'bold'),
text="\n\nWelcome At The CopyAssignment.com").grid(row=8,columnspan=6)

#Submit Button
setbtn=Button(root,text="Split your Bill!",command=billsplit,bg="#D4AC0D",
fg="Black",font = ('arial', 19, 'bold')).grid(row=9,columnspan=4)

mainloop() 

Output:

Output for Item Price Generator in Python

The project is completed in very simple stages! You may now use this Project to split bills with friends and coworkers. You are welcome to execute your code right now to see how it works. If you get stuck or have issues with your code, please leave a remark below.


Also Read:

Share:

Author: Ayush Purawr