【发布时间】:2021-12-26 03:46:21
【问题描述】:
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
我不明白如何解决错误信息:
运行错误 回溯(最近一次通话最后): 文件“tester.python3”,第 55 行,在 餐厅.append(x) AttributeError: 'dict' 对象没有属性 'append'
我正在尝试将用户输入的餐厅及其功能添加到餐厅列表中。
【问题讨论】:
-
append()仅适用于列表。字典没有append()方法。这回答了你的问题了吗? Python AttributeError: 'dict' object has no attribute 'append' -
我需要删除 dict() 并改为创建一个列表吗?
-
视情况而定。你需要使用字典吗?如果是这样,您可以查看我之前评论中的链接。否则,只需切换到一个列表。
-
不,我不认为我需要使用字典,我不确定如何创建列表,但我会检查您的链接并尝试解决