Milan Harindran Milan Harindran - 2 months ago 15
Python Question

fix thsi please

I keep getting errors like this. It is a restaurant code, in which the menu is printed and orders are taken but error is in writing the order and cost in a file text,

I have tried to make a menu in form of dictionary, but can't write the data in text file

class restaurant():
def __init__(self):
self.name = ""
self.menu = {}
self.order = []
self.bill = 0
def print_menu(self):
print "MENU CARD"
self.menu = {'BBQ Grill':'50','Chicken Gollati':'80','French fries':'60',
'Hara Bara Kabab':'90','Makani Special Dum Biriyani':'100',
'Egg Jumbo Sandwich':'120','Roasted Prawn Salad':'90',
'Parathas':'80','Turkish Barbeque Plate':'100'}
for item in self.menu:
print item,"-",self.menu[item]
def takeorder(self):
f1 = open("billlog.txt","w")
print "What would you like to order?"
ans = "y"
while ans == "y":
food = raw_input("enter order - ")
self.bill += int(self.menu[food])
ans = raw_input("go on?(y/n): ")
f1.write(food)
f1.write("\t\t")
f1.write(self.bill)
print food,"\t\t\t",self.bill
f1.close()
def readfilebilllogs(self):
f1 = open("billlog.txt","r")
f1.read()
f1.close()
r = restaurant()
r.print_menu()
r.takeorder()
r.readfilebilllogs()

Answer

Your code has multiple errors. Try this it should work. I tried on python3 and modified for python2.7 so there could be some syntax error. I've explained the errors in comment

class restaurant():
  def __init__(self):
    self.name = ""
    self.menu = {}
    self.order = []
    self.bill = 0
  def print_menu(self):
    print "MENU CARD"
##This should be self.menu instead of just menu. If you use just menu it's a local variable which can't be used from other function
    self.menu = {'BBQ Grill':'50','Chicken Gollati':'80','French fries':'60',
            'Hara Bara Kabab':'90','Makani Special Dum Biriyani':'100',
             'Egg Jumbo Sandwich':'120','Roasted Prawn   Salad':'90',
              'Parathas':'80','Turkish Barbeque Plate':'100'}
#Again self.menu
    for item in self.menu:
      print item,"-",self.menu[item]
  def has_item(self):
    name = raw_input("Enter name of costumer: ")
    food = raw_input("Enter order: ")
    for i in self.menu:
      if i == food:
        print "Yes"
      else:
        print "No"

# The first parameter is always instance of the class (self).
  def takeorder(self):
    print "What would you like to order?"
    ans = "y"
    while ans == "y":
      food = raw_input("enter order - ")
# Instead of bill it should be self.bill
#Convert string value of cost to int while adding
      self.bill += int(self.menu[food])
      ans = raw_input("go on?(y/n): ")
    print self.bill
r = restaurant()
r.print_menu()
r.takeorder()