r/learnpython • • Sep 15 '26

Why i can't make a class

It should work, but some reason doesn't.

I was doing Python Crash Course 3rd edition and am stuck in classes.

Because i cant upload photos, i wrote the code:

class Restaurant:

def __init__(self, restaurant_name, cuisine_type):

    self.restaurant_name=restaurant_name

    self.cuisine_type=cuisine_type

def describe_restaurant(self):

    print(self.restaurant_name)

    print(self.cuisine_type)

def open_restaurant(self):

    print("the restaurant is open")

restaurant=Restaurant("Ratatouille restaurant", "ratatouille")

describe_restaurant(restaurant)

open_restaurant()

This is the example in the book:

class Dog:

def __init__(self, name, age):

    self.name=name

    self.age=age

def sit(self):

    print(f"{self.name} is now sitting")

def roll_over(self):

    print(f"{self.name} rolled over")

my_dog=Dog("Willie", 6)

print(f"My dog's name is {self.name}")

print(f"My dog is {self.age} years old")

my_dog.sit()

my_dog.roll_over()

your_dog=Dog("Lucy", 3)

print(f"Your dog's name is {your_dog.name}.")

print(f"your dog is {your_dog.name} years old.")

your_dog.sit()

your_dog.roll_over()

And these are the errors pydroid shows for each one:

Restaurant:

Traceback (most recent call last):

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>

start(fakepyfile,mainpyfile)

~~~~~^^^^^^^^^^^^^^^^^^^^^^^

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start

exec(open(mainpyfile).read(), __main__.__dict__)

~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "<string>", line 1, in <module>

File "<string>", line 10, in Restaurant

NameError: name 'Restaurant' is not defined

[Program finished]

Dog:

Traceback (most recent call last):

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>

start(fakepyfile,mainpyfile)

~~~~~^^^^^^^^^^^^^^^^^^^^^^^

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start

exec(open(mainpyfile).read(), __main__.__dict__)

~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "<string>", line 1, in <module>

File "<string>", line 9, in Dog

NameError: name 'Dog' is not defined

[Program finished]

0 Upvotes

42 comments sorted by

View all comments

1

u/FoolsSeldom Sep 15 '26

You need to create a new file in PyDroid. In the menu (the three short horizontal parallel lines at top left), you have options for Interpreter and Terminal but you don't want to use either of these.

You need to tap the folder icon near the top right and select new, and you can then enter your lines of code. Tapping the folder icon again gives you save and save as options. Once saved, you can run the code by clicking the play icon near the bottom right.

1

u/Upper_Trouble_8526 Sep 15 '26

That is how i did it

1

u/FoolsSeldom Sep 15 '26

Did you have a >>> prompt? What did you name the file?

1

u/Upper_Trouble_8526 Sep 15 '26

No. I named them Restaurant and Dog.

1

u/FoolsSeldom Sep 15 '26

Ok. I have a good guess, and it is a guess because you've messed up the code formatting in your post.

It looks like you've tried to create an instance of Restaurant and instance of Dog whilst still inside the corresponding definitions (i.e. indented at the same level as the class definition code). You have to do the instantiation after the definitions.

1  class Dog:
2      def __init__(self, name, age):
3          self.name = name
4          self.age = age
5      def sit(self):
6          print(f"{self.name} is now sitting")
7      def roll_over(self):
8          print(f"{self.name} rolled over")
9      my_dog = Dog("Willie", 6)

If I have untangled correctly, in the above, line 9, is indented inside the class. That line should not be indented.

1

u/Upper_Trouble_8526 Sep 15 '26

It worked

1

u/FoolsSeldom Sep 16 '26

Excellent. Enjoy.

Would have been resolved quicker if you had been able to format the code in Reddit properly, but it is a bit weird to learn at first if you aren't familiar with markdown format and reddit's dialect of it. I use markdown all the time as my default on reddit and with Obsidian to keep my notes.