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行,在restaurants.append(x) AttributeError:'dict‘对象中没有属性'append’
我试图添加一个用户输入餐厅和它的特点到餐厅列表。
发布于 2021-11-14 18:36:13
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!")
breakhttps://stackoverflow.com/questions/69965877
复制相似问题