4 Replies - 703 Views - Last Post: 24 March 2015 - 01:21 AM Rate Topic: -----

#1 CyberPat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 25-September 14

Odd indentaion error?

Posted 23 March 2015 - 06:33 AM

Hello there. I have came across a piece of code which is used to teach Python novice programmers about OOP constructs. Here is the code:

class Car():
    condition = "new"

    
print (my_car.condition)

    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg

my_car = Car("DeLorean", "silver", 88)

print (my_car.model)
print (my_car.color)
print (my_car.mpg)




When i was attempting to run the code, i got unexpected indent error.

After moving the
print(my_car.condition)
function to the bottom of program, i didn't received the error anymore. Now i wanted to ask, since the print function has had nothing to do with indentation, why has the Python been giving error?

Here is the working code:

class Car(object):
    condition = "new"

    
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg

my_car = Car("DeLorean", "silver", 88)

print (my_car.model)
print (my_car.color)
print (my_car.mpg)
print (my_car.condition)


Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Odd indentaion error?

#2 andrewsw   User is online

  • Entwickler
  • member icon

Reputation: 6602
  • View blog
  • Posts: 26,903
  • Joined: 12-December 12

Re: Odd indentaion error?

Posted 23 March 2015 - 06:54 AM

print() appearing unindented on line 5 indicates that the class-definition (for Car) is complete. It is then the def that follows this line that has the indentation error.

The print() statement also needed to be moved because my_car is an "unresolved reference" if it is referenced on line 5.
Was This Post Helpful? 4
  • +
  • -

#3 pythonuser007   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 25-January 14

Re: Odd indentaion error?

Posted 23 March 2015 - 06:56 AM

Class Car ends when you stop indenting, so the unexpected indent is to do with the next line of code where it defines the init routine for the class. This is cut off from the class so you have indented some undefined function when it wasn't expected.

Now when you move the print statement you then rejoin the init function to the class as its the same indent so belongs to the class now.

Your program then runs correctly.

Python uses indenting to properly define statement blocks such as class blocks as well as function blocks. When you change the indent you change whatever you were doing such as coding a function or class.

Not a good explanation but i hope it helped.
Was This Post Helpful? 4
  • +
  • -

#4 CyberPat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 25-September 14

Re: Odd indentaion error?

Posted 23 March 2015 - 10:09 PM

View Postandrewsw, on 23 March 2015 - 06:54 AM, said:

print() appearing unindented on line 5 indicates that the class-definition (for Car) is complete. It is then the def that follows this line that has the indentation error.

The print() statement also needed to be moved because my_car is an "unresolved reference" if it is referenced on line 5.


Thanks for the great answer as well as time. What is an "unresolved reference"? Thanks again.

View Postpythonuser007, on 23 March 2015 - 06:56 AM, said:

Class Car ends when you stop indenting, so the unexpected indent is to do with the next line of code where it defines the init routine for the class. This is cut off from the class so you have indented some undefined function when it wasn't expected.

Now when you move the print statement you then rejoin the init function to the class as its the same indent so belongs to the class now.

Your program then runs correctly.

Python uses indenting to properly define statement blocks such as class blocks as well as function blocks. When you change the indent you change whatever you were doing such as coding a function or class.

Not a good explanation but i hope it helped.


The answer is great and quite helpful! Thanks for the answer as well as time.

This post has been edited by CyberPat: 23 March 2015 - 10:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 890
  • View blog
  • Posts: 3,591
  • Joined: 13-June 14

Re: Odd indentaion error?

Posted 24 March 2015 - 01:21 AM

View PostCyberPat, on 24 March 2015 - 05:09 AM, said:

Thanks for the great answer as well as time. What is an "unresolved reference"? Thanks again.


You're trying to refer to something (e.g. a variable, class or function) that doesn't exist. In your case, this was because you tried to use a variable (my_car) before declaring it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1