不理解 Python 中的错误消息:属性错误:'dict'对象没有属性'append'



任务我需要做

class Restaurant:
def __init__(self,
name, website, cuisine):
self.name = name
self.website = website
self.cuisine = cuisine

dominoes = Restaurant("Dominoes", "www.dominoes.co.uk", "pizza")
yosushi = Restaurant("Yo Sushi!", "www.yosushi.co.uk", "sushi")
fiveguys = Restaurant("Five Guys", "www.fiveguys.co.uk" ,"burger")
restaurants = dict()
restaurants["Dominoes"] = dominoes
restaurants["Yo Sushi!"] = yosushi
restaurants["Five Guys"] = fiveguys
in_loop = True
while in_loop:
print("CS1822 Restaurant DB")
print("1. Display restaurant list")
print("2. Add a restaurant")
print("3. Exit")
choice = input("Please enter your choice: ")
if choice == "1":
for name in restaurants:
restaurant = restaurants[name]
print(name, "-", restaurant.website, "-", restaurant.cuisine)

elif choice == "2":
name = input("Enter restaurant name: ")
website = input("Enter website: ")
cuisine = input("Enter cuisine: ")
x = Restaurant(name, website, cuisine)
restaurants.append(x)

else:
print("Goodbye!")
break

我不明白如何解决错误消息:

运行错误追踪(最近一次通话(:文件">测试器.python3";,第55行,in餐馆.append(x(AttributeError:"dict"对象没有属性"append">

我正在尝试将用户输入的餐厅及其功能添加到餐厅列表中。

class Restaurant:
def __init__(self,
name, website, cuisine):
self.name = name
self.website = website
self.cuisine = cuisine

dominoes = Restaurant("Dominoes", "www.dominoes.co.uk", "pizza")
yosushi = Restaurant("Yo Sushi!", "www.yosushi.co.uk", "sushi")
fiveguys = Restaurant("Five Guys", "www.fiveguys.co.uk", "burger")
restaurants = dict()
restaurants["Dominoes"] = dominoes
restaurants["Yo Sushi!"] = yosushi
restaurants["Five Guys"] = fiveguys
in_loop = True
while in_loop:
print("CS1822 Restaurant DB")
print("1. Display restaurant list")
print("2. Add a restaurant")
print("3. Exit")
choice = input("Please enter your choice: ")
if choice == "1":
for name in restaurants:
restaurant = restaurants[name]
print(name, "-", restaurant.website, "-", restaurant.cuisine)
elif choice == "2":
name = input("Enter restaurant name: ")
website = input("Enter website: ")
cuisine = input("Enter cuisine: ")
x = Restaurant(name, website, cuisine)
restaurants[name]=x ### You can't use 'append()', because you are using 'dict' in this code, so for 'dict' you need this string
else:
print("Goodbye!")
break

最新更新