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

Show parent comment

-1

u/Upper_Trouble_8526 Sep 15 '26

Im writing in pydroid, should i get another app?

1

u/danielroseman Sep 15 '26

I didn't say that. I said that if you're writing in a REPL, you should keep the definition separate from where you use the definition.

1

u/Upper_Trouble_8526 Sep 15 '26 edited Sep 15 '26

How do i switch to editor or keeps definition and its use separate, aren't the definition and its usage unindented

1

u/danielroseman Sep 15 '26

I don't know pydroid. But it's quite clear from your post that they are not properly separated.

All you need to do is to write the definition and execute it; only then write the code that uses the class and execute that.

But as I say, this is not how you really write code; you do that in an actual editor, rather than typing directly into a shell.

1

u/Upper_Trouble_8526 Sep 15 '26

In the playstore the app was classified as an editor, so i must treat the classes like functions modules because it works like a shell

1

u/ProgramsFun Sep 15 '26 edited Sep 15 '26

You are writing in repl or terminal write in the editor it gives you bro and also please atleast use ai to format the post correctly are you expecting people to read your post with so much noise and unformatted

1

u/Upper_Trouble_8526 Sep 15 '26

How do i do that, the original text didn't have backslashes

-2

u/ProgramsFun Sep 15 '26 edited Sep 15 '26

Use ai bro just upload the post to ai and tell it to format it will clean the noise

Edit:- if people are downvoting for the use of ai 🤡 go ahead and downvote me... If you can't use ai to your advantage then you are the only one who is being left behind... (Not supporting vibe coding)