簡體   English   中英

不理解 Python 中的錯誤消息:AttributeError: 'dict' object has no attribute 'append'

[英]Not understanding error message in Python: AttributeError: 'dict' object has no attribute '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

我不明白如何解決錯誤消息:運行錯誤回溯(最近一次調用最后一次):文件“ tester .python3”,第 55 行,在 restaurant.append(x) AttributeError: 'dict' object has no attribute '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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM