0

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

I do not understand how to solve the error message:

Run error Traceback (most recent call last): File "tester.python3", line 55, in restaurants.append(x) AttributeError: 'dict' object has no attribute 'append'

I am trying to add a user input restaurant and its features to the list of Restaurants.

Prajwal Kulkarni
  • 1,480
  • 13
  • 22
IamRyuu
  • 1
  • 1
  • 2
    `append()` only works on lists. Dictionaries don't have an `append()` method. Does this answer your question? [Python AttributeError: 'dict' object has no attribute 'append'](https://stackoverflow.com/questions/48234473/python-attributeerror-dict-object-has-no-attribute-append) – Sylvester Kruin Nov 14 '21 at 18:18
  • Do i need to remove the dict() and create a list instead? – IamRyuu Nov 14 '21 at 18:19
  • 1
    It depends. Do you need to use a dictionary? If so, you can check out the link in my previous comment. Otherwise, just switch to a list. – Sylvester Kruin Nov 14 '21 at 18:20
  • No I don't believe I need to use a dictionary, im not sure how to create a list but i'll check your link out and try and work it out – IamRyuu Nov 14 '21 at 18:29

1 Answers1

-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
dtroyan
  • 101
  • 1
  • 1
    Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it answers the specific question being asked. See [answer]. – ChrisGPT was on strike Nov 14 '21 at 19:00
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 14 '21 at 19:57