2016-09-17 45 views
-6

我不斷收到這樣的錯誤。這是一個餐廳代碼,其中菜單打印和訂單被採取,但錯誤是在寫入文件中的訂單和成本文本,修復thsi請

我試圖做一個字典形式的菜單,但不能寫文本文件

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() 
+0

當出現錯誤時,代碼是不正確的。錯誤也很明顯:你在分配一個值之前引用了一個變量。你可能想'self.bill' ... –

+0

請將錯誤信息添加爲文本,而不是圖像。 – Matthias

回答

1

您的代碼有多個錯誤。嘗試這應該工作。我嘗試了python3並修改了python2.7,所以可能會出現一些語法錯誤。我已在說明中解釋錯誤

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() 
0

更改行中的數據:

bill += menu[food] 

到:

self.bill += menu[food] 
1

A C偶然出現的錯誤:

  • Turkish Barbeque Plate'這是在開頭缺少一個撇號。
  • 您在構造函數中定義了您的字段,以便在類中的其他方法中使用它們,您需要以self作爲前綴。這就是爲什麼你需要將自我傳遞給所有其他方法。此外,採購方法需要通過自己而不是print_menu。
  • 你的菜單項的值是str類型,而在這一行bill += menu[food]你正在添加str來計費這是一個int。這是行不通的。無論哪種變化值爲int或改變bill += int(menu[food])
0

此行中我看到的第一個問題是,在你的menu變量有撇號的土耳其燒烤板,它會破壞你的字典之前失蹤。其次,你指的是賬單在創建之前。

我已經解決了一些事情,下面的工作。

class restaurant(): 
    def __init__(self): 
     self.name = "" 
     self.order = [] 
     self.bill = 0 
     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} 

    def print_menu(self): 
     print ("MENU CARD") 
     for item in self.menu: 
      print '{} - {}'.format(item, self.menu[item]) 

    def has_item(self): 
     name = raw_input("Enter name of customer: ") 
     food = raw_input("Enter order: ") 
     for item in self.menu(): 
      if item == food: 
       print "Yes" 
      else: 
       print "No" 

    def take_order(self): 
     print "What would you like to order? " 
     response = 'yes' 
     while response.lower() == 'yes':  #.lower() to remove caps from response 
      food = raw_input("Enter Order: ") 
      self.bill += self.menu[food] 
      response = raw_input("Go on? (Yes or No): ") 
     print self.bill 

r = restaurant() 
r.print_menu() 
r.take_order()