Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中添加到列表(从类实例)_Python_Python 3.x - Fatal编程技术网

在python中添加到列表(从类实例)

在python中添加到列表(从类实例),python,python-3.x,Python,Python 3.x,我已经用python定义了一个类,其中包含各种餐厅及其详细信息。我现在的任务是基于该类实例创建一个列表变量,允许用户添加或打印列表。我面临的问题是在(ans==2)语句中,用户可以在其中添加他们的新餐厅及其详细信息 这是密码 class Restaurant: def __init__(self,name,website,cuisine): self.name = name self.website = website self.cuisi

我已经用python定义了一个类,其中包含各种餐厅及其详细信息。我现在的任务是基于该类实例创建一个列表变量,允许用户添加或打印列表。我面临的问题是在(ans==2)语句中,用户可以在其中添加他们的新餐厅及其详细信息

这是密码

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")
我正在做的部分是下一个,并且已经被告知上面的类已经被定义了

def menu():
    restaurants = [dominoes, yosushi, fiveguys]
    ans= int(input("CS1822 Restaurant DB\n1. Display restaurant list\n2. Add a restaurant\n3. 
Exit\nPlease enter your choice:"))

while (ans != 3):

    if (ans == 1):
        for i in restaurants:
            print(i.name,i.website,i.cuisine)
        menu()

    if (ans == 2):
        new_res = (input("Enter restaurant name: "))
        restaurants.append(new_res)
        new_res.website = input("Enter website: ")
        new_res.cuisine = input("Enter cuisine: ")
        menu()

    if (ans == 3):
        print("Goodbye!")
这就是我收到的错误

***Error***
Traceback (most recent call last):
  File "__tester__.python3", line 22, in <module>
    while (ans != 3):
NameError: name 'ans' is not defined
***错误***
回溯(最近一次呼叫最后一次):
文件“\uuuu tester\uuuu.python3”,第22行,在
而(ans!=3):
NameError:未定义名称“ans”
我需要帮助的两个问题是: a) 我的while循环说变量ans是未定义的,不是 b) 我的if(ans==2)语句完全错误,根本没有运行,说明new_res不是可以追加的对象


谢谢,缩进使while循环不再是menu()函数的一部分。它之所以说它未定义,是因为while循环无法访问ans,因为它只存在于menu()函数中

def menu():
    restaurants = [dominoes, yosushi, fiveguys]
    ans= int(input("CS1822 Restaurant DB\n1. Display restaurant list\n2. Add 
          a restaurant\n3. Exit\nPlease enter your choice:"))

    while (ans != 3):

        if (ans == 1):
            for i in restaurants:
                print(i.name,i.website,i.cuisine)
            menu()

        if (ans == 2):
            new_res = (input("Enter restaurant name: "))
            restaurants.append(new_res)
            new_res.website = input("Enter website: ")
            new_res.cuisine = input("Enter cuisine: ")
            menu()

        if (ans == 3):
            print("Goodbye!")
以下三行是ans==2的问题。您将字符串变量'new_res'视为餐厅对象

restaurants.append(new_res)
new_res.website = input("Enter website: ")
new_res.cuisine = input("Enter cuisine: ")
您需要创建一个新的餐厅对象,然后附加餐厅列表

if (ans == 2):
    res_name = input("Enter restaurant name: ")
    res_website = input("Enter website: ")
    res_cuisine = input("Enter cuisine: ")
    new_res = Restaraunt(res_name, res_website, res_cuisine)
    restaurants.append(new_res)
    menu()