Create an instance of a class


#1


This is the error message I get when I use the code below:

Oops, try again. You haven't created a Car object named my_car; make sure you create my_car after defining Car.

I can't see what is wrong with this seemingly simple exercise. Help please!


class Car(object):
    
    my_car=Car()


#2

Two things wrong:
First, your indentation is off. You can't create a new object, inside of defining what that kind of object is. That's not a good way of wording it, but a python design choice is that everything indented after a colon is inside of that if statement/function/class/etc.. (Also not a good explanation.) Basically, unindent

this line, and then you need to write pass inside of the class Car(object): because that's a keyword that kind of place holds. It tells python to ignore the fact that nothing is inside of something that would typically need something inside of it to define it.

I hope that you got something out of this, it's very late so i'm probably not sounding too bright.


#3

Ah, I don't know why I thought I had to indent the code of creating an instance. Thank you so much!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.