首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不理解Python中的错误消息: AttributeError:'dict‘对象没有属性'append’

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

Stack Overflow用户
提问于 2021-11-14 18:16:17
回答 1查看 512关注 0票数 0

我需要做的事

代码语言:javascript
复制
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’

我试图添加一个用户输入餐厅和它的特点到餐厅列表。

EN

回答 1

Stack Overflow用户

发布于 2021-11-14 18:36:13

代码语言:javascript
复制
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
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69965877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档