我已经写了代码,但是当打印账单时,总金额是错误的

时间:2016-10-28 08:42:07

标签: python

*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): ")*

例如,我订购了4个烧烤炉,50个Qr和3个百事可乐,25个Qr,我得到的总量是675而不是275.代码有什么问题?我不知道错误是什么。

1 个答案:

答案 0 :(得分:2)

代码中的错误位于以下部分:

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

您将整个帐单乘以订购数量。因此:

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

然后你添加了3个Pepsis:

<ul class="gallery gallery-animated">  <!--    --></ul>

这就是你得到675的原因。相反,你只需要这样做:

.gallery {
    display: inline-block;
    width: auto;
    height: 100%; // This one removes the whitespace.
    margin: 0;
    list-style-type: none;
}