I have defined a class in python containing a variety of restaurants and details on them. My task is now to create a list variable based on that class instance, allowing the user to add or print the list. The problem I'm facing is in the (ans == 2) statement where the user can add their new restaurants and details of them.
This is the code
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")
The part I'm doing is this next one and have been told that the above class has already been defined.
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!")
This is the error I recieve
***Error***
Traceback (most recent call last):
File "__tester__.python3", line 22, in <module>
while (ans != 3):
NameError: name 'ans' is not defined
My two problems I would please like help for are: a) my while loop says that variable ans is undefined and is not b) my if (ans == 2) statement is completely wrong and is not running at all, stating that new_res is not an object that can be appended.
Thanks