Reputation: 23
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
Upvotes: 0
Views: 69
Reputation: 375
The indentation as it is makes the while loop NOT a part of the menu() function. The reason why it says it's undefined is because the while loop doesn't have access to ans because it only exists within the menu() function.
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!")
These 3 following lines are the issue with ans == 2. You're treating the string variable 'new_res' as a Restaurant object.
restaurants.append(new_res)
new_res.website = input("Enter website: ")
new_res.cuisine = input("Enter cuisine: ")
You need to create a new Restaurant object and then append the restaurant list.
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()
Upvotes: 1