#Python crash course, working with classes and instances

15 messages · Page 1 of 1 (latest)

onyx nebulaBOT
#

@compact mica

onsinn Uploaded Some Code

having issues with the code in the book, no output or says its undefined? what missing?

Attachment: yetmorecars.py
class Car():
    """A simple attempt to represent a car."""
    
    def  __init__(self, make, model, year):
        """"Initialize attributes to desribe a car."""
        self.make = make
        self.model = model
        self.year = year
        
    def get_descriptive_name(self):
            """Return a neatly formatted descriptive name."""
            long_name = f"{self.year} {self.make} {self.model}"
            return long_name.title()
    my_new_car = Car('audi', 'a4', 2024)
    print(my_new_car.get_descriptive_name())
    
    Car =Car
    
coarse canopy
#

Your non-class code is inside your class?

compact mica
#

you tell me thats, whats in the book

coarse canopy
#

What do you mean?

compact mica
#

that is whats written in my python crash course book, had same problem with the previous section. I either get no output or its says what came after class is undefined.

coarse canopy
#

Did you check that the indentation is the same as in the book?

compact mica
#

yes, am using visual code, it would say if there was a problem

coarse canopy
#

Not with this kind of error

compact mica
#

then were is the error?

coarse canopy
#

Unless your book has errors, the code you sent us should be different to the code in the book

compact mica
#

so how do I fix it?

coarse canopy
#

Get rid of the indentation for your last three lines

#

Those are lines that shouldn't be a part of the class definition

#

But for some reason are inside it

compact mica
#

thanks, got output now