【问题标题】:Not understanding error message in Python: AttributeError: 'dict' object has no attribute 'append'不理解 Python 中的错误消息:AttributeError: 'dict' object has no attribute 'append'
【发布时间】:2021-12-26 03:46:21
【问题描述】:

Task I need to do

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() 并改为创建一个列表吗?
  • 视情况而定。你需要使用字典吗?如果是这样,您可以查看我之前评论中的链接。否则,只需切换到一个列表。
  • 不,我不认为我需要使用字典,我不确定如何创建列表,但我会检查您的链接并尝试解决

标签: python list append


【解决方案1】:
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

【讨论】:

  • 代码在附有解释时会更有帮助。 Stack Overflow 是关于学习的,而不是提供 sn-ps 来盲目复制和粘贴。请edit您的问题并解释它如何回答所提出的具体问题。见How to Answer
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2014-05-18
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
  • 2022-12-03
相关资源
最近更新 更多