我不断收到这样的错误。这是一个餐厅代码,其中打印了菜单并接受了订单,但是在将订单和费用写入文件文本时出错,
我试图以字典的形式制作菜单,但无法在文本文件中写入数据
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()
您的代码有多个错误。试试这个,它应该可以工作。我尝试使用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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句