0

I'm new to making classes and I'm trying to complete exercise 9-1 in my 'Python Crash Course' book where the last part of the question asks me to call back my method but I end up getting

'not defined error' for describe_restaurant().

Here is my code:

class Restaurant():
    def __init__(self, r_name, c_type):
        self.r_name = r_name
        self.c_type = c_type

    def describe_restaurant():
        print(self.r_name.title())
        print(self.c_type.title())

    def open_restaurant():
        print(self.r_name + " is now open!")

Restaurant = Restaurant('Joe\'s Sushi', 'sushi')

print(Restaurant.r_name)
print(Restaurant.c_type)

describe_restaurant()
open_restaurant()

I thought that describe_restaurant shouldn't need to be defined though because I'm calling it out as a function to use?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • You should firstly create `Restaraunt` object and then calling `describe_restaurant` from newly created object – vishes_shell Sep 20 '16 at 19:36
  • It's a class function. You need to call the class function with a class object. – MooingRawr Sep 20 '16 at 19:37
  • 3
    You should revisit the lesson plan on Classes. Here is another look via the official tutorial: https://docs.python.org/3/tutorial/classes.html – idjaw Sep 20 '16 at 19:37
  • You need to add the `self` parameter to your method definitions inside the `Restaurant` class. Also, don't shadow your class name with an instance! Finally, you need to call the method using an instance. – juanpa.arrivillaga Sep 20 '16 at 19:39

1 Answers1

2

Try:

class Restaurant():
    def __init__(self, r_name, c_type):
        self.r_name = r_name
        self.c_type = c_type

    def describe_restaurant(self):
        print(self.r_name)
        print(self.c_type)

    def open_restaurant(self):
        return "{} is now open!".format(self.r_name)

restaurant = Restaurant('Joe\'s Sushi', 'sushi')

print(restaurant.r_name)
print(restaurant.c_type)

restaurant.describe_restaurant()
restaurant.open_restaurant()

You need to create a class instance and call it's functions. In addition, as mentioned in the comments, you need to pass self to the instance methods. A short explanation of this can be found here.

albert
  • 8,027
  • 10
  • 48
  • 84