Milan Harindran Milan Harindran - 1 month ago 12
Python Question

I have written the code but when the bill is printed the total amount is wrong

*class restaurant():
def __init__(self):
self.drinks = {}
self.name = ""
self.menu = {}
self.bill = 0
self.food = ""
self.pay = 0
self.quantity = 0
#This function is used to show the menu card and take order.
def showmenuandtakeorder(self):
print "Welcome to Titanic Hotel."
print "-------------------------------------------------------------"
print "MENU CARD"
print "-------------------------------------------------------------"
self.menu = {'BBQ Grill':'50','Mutton Fry':'70','French fries':'30',
'Kebab':'30','Dum Biriyani':'70',
'Egg Sandwich':'10','Roasted Prawns':'79',
'Parathas':'5','Barbeque Plate':'100','Gulab Jamun':'120','Gobi Manchurian':'250'}
for item in self.menu:
print item,"-",self.menu[item]
print "--------------------------------------------------------------"
print "DRINKS"
print "--------------------------------------------------------------"
self.drinks = {'Diet Coke':'15','Coke':'10','7-up Ice':'25','Miranda':'20','Fanta':'15','Pepsi':'25',
'Sharjah shake':'50','Titanic shake':'35'}
for item in self.drinks:
print item,"-",self.drinks[item]
print "--------------------------------------------------------------"
f1 = open("bills.txt","w")
self.name = raw_input("Enter name of costumer: ")
print "What would you like to order %s?" %self.name
ans = "y"
f1.write("BILL") #The below mentioned code is for printing the bill from a txt file
f1.write("\n")
f1.write("Name of costumer - ")
f1.write(self.name)
f1.write("\n")
f1.write("Item")
f1.write("\t")
f1.write("\t")
f1.write("Cost")
f1.write("\t")
f1.write("\t")
f1.write("Quantity")
f1.write("\n")
while ans == "y":
food = raw_input("enter order - ")
quantity = input("How much do you need sir?: ")
if food in self.menu:
self.bill += int(self.menu[food])
self.bill = self.bill * quantity
f1.write(food)
f1.write("\t")
f1.write("\t")
f1.write(str(self.menu[food]))
f1.write("\t")
f1.write("\t")
f1.write(str(quantity))
f1.write("\n")
elif food in self.drinks:
self.bill += int(self.drinks[food])
self.bill = self.bill * quantity
f1.write(food)
f1.write("\t")
f1.write("\t")
f1.write(str(self.drinks[food]))
f1.write("\t")
f1.write("\t")
f1.write(str(quantity))
f1.write("\n")
else:
print "Sorry item does not exist on the menu."
ans = raw_input("Anything else sir?(y/n): ")
print "Sir your total is ", self.bill , "QR/-"
f1.write("Total")
f1.write("\t")
f1.write(str(self.bill))
f1.close()
print "Here's your bill......"
print "\n"
print "H:O:T:E:L T:I:T:A:N:I:C"
print "---------------------------------"
self.readfilebilllogs()
print "---------------------------------"
self.pay = input("Enter amount to be paid: ")
if self.pay == self.bill:
print "Thank you sir"
elif self.pay < self.bill:
print "Sorry sir you need to pay the full amount"
else:
change = self.pay - self.bill
print "Thanks..here's your balance" , change , "Rs/-"
def readfilebilllogs(self):
f1 = open("bills.txt","r")
f2 = f1.read()
print f2
f1.close()
def feedback(self):
string = raw_input("Enter feedback: ")
f2 = open("feedback.txt","w")
f2.write(string)
print "Thank you for the feedback..."
f2.close()
r = restaurant()
opt = "y"
while opt == "y":
print "HOTEL TITANIC RECEPTION"
print "1. Show menu and take order"
print "2. Print bill"
print "3. Give feedback"
print "4. Contact Us"
print "5. Thank you for coming"
ch = input("Enter choice(1-6): ")
if ch == 1:
r.showmenuandtakeorder()
elif ch == 2:
r.readfilebilllogs()
elif ch == 3:
r.feedback()
elif ch == 4:
print "CONTACT US"
print "\n"
print "Location: Matar Qadeem"
print "\n"
print "Phone no.: 9748854855255"
print "\n"
print "contact email: titan123@yahoo.com"
elif ch == 5:
exit()
else:
break
opt = raw_input("Anything else?(y/n): ")*


For instance, I ordered 4 BBQ Grill which is 50 Qr and 3 Pepsi which is 25 Qr and the total I got is 675 instead of getting 275. What is wrong in the code? I have no idea what the error is.

Answer

The error in your code is in the below section:

self.bill += int(self.menu[food])
self.bill = self.bill * quantity

You are multiplying the whole bill by the quantity ordered. Therefore:

self.bill = 50 # (BBQ Grill)
self.bill = 50 * 4 # (4 x BBQ Grill)
self.bill = 200

Then you added 3 Pepsis so:

# self.bill is currently 200
self.bill += 25 # = 225 (Adding a Pepsi)
self.bill * 3 # = 675 (Multiplying the WHOLE bill by 3)

That why you got 675. Instead you just need to do this:

self.bill += int(self.menu[food]) * quantity
Comments