I want to create a tool that allows a user to input a list of restaurant details to be stored and printed back out when complete.
My code follows:
#Create restaurant class with restaurant object and describe_restaurant function
class restaurant:
def __init__(self, n, restaurant_name, cuisine_type):
self.n = n
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print("{} serves {}".format(restaurant.restaurant_name, restaurant.cuisine_type))
#localplaces will contain the restaurant objects
localplaces = []
n = len(localplaces)
active = True
while active:
localplaces.append(restaurant(n, input("Restaurant Name: "), input("Cuisine type: ")))
repeat = input("Do you want to add another item?: ")
if repeat.strip().lower() in ["n", "no"]:
active = False
else:
while repeat.strip().lower() not in ['y', 'yes', 'n', 'no']:
repeat = input("Add another item? [y/n]: ")
#loop through localplaces printing the object info as defined by the
for restaurant in localplaces:
print("\n" + restaurant.describe_restaurant().format)
I get the following error when running this code (including my input):
Restaurant Name: bobs
Cuisine type: burgers
Do you want to add another item?: n
bobs serves burgers
Traceback (most recent call last):
File "C:/Users/nicholl/PycharmProjects/untitled/Coordinates.py", line 24, in <module>
print("\n" + restaurant.describe_restaurant().format)
AttributeError: 'NoneType' object has no attribute 'format'
Meanwhile if I remove .format
from my for
loop (including my input):
Restaurant Name: bobs
Cuisine type: burgers
Do you want to add another item?: n
bobs serves burgers
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/Test/Test11111c.py", line 24, in <module>
print("\n" + restaurant.describe_restaurant())
TypeError: can only concatenate str (not "NoneType") to str
Which confused me by giving me what I assume is a returned describe_restaurant()
but if I input a second restaurant I receive the same error, excluding the second restaurant but it will still return the first restaurant as above.
Using Python 3.7 in PyCharm, let me know if I need to clarify anything.
You're referencing restaurant
in this function, but it is a reference to the class , which doesn't have restaurant_name
or cuisine_type
attributes:
def describe_restaurant(self):
print("{} serves {}".format(restaurant.restaurant_name, restaurant.cuisine_type))
Instead, use self
, which will be a reference to the object itself:
def describe_restaurant(self):
print("{} serves {}".format(self.restaurant_name, self.cuisine_type))
A couple of other stylistic tips:
Restaurant
instead of restaurant
(see PEP 8 ) describe
instead of describe_restaurant
; that method is part of your class and the context should make it clear that it relates to restaurants
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.