Python Forum
Some Confusing Program Errors (Newbie stuff)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Some Confusing Program Errors (Newbie stuff)
#1
Hello All!
So I am busy teaching myself Python and I am going through bits of code with supposed errors.
There is one particular one I am stuck with, that needs me to correct the class definitionin the code.... though I still have trouble making heads or tails of certain elements.
Could anyone please help?

class Car(object):
    numwheels = 4
    def display(self):
        print("Make:", self.make)
        print("Colour:", self.colour)
        print("Wheels:", self.numwheels)
#MainProgram
c0bj1 = Car("Ford","Black")
c0bj1.display()
Reply
#2
Your class is missing constructor, which in Python is declared with __init__() method.

class Car(object):
    def __init__(self,make,colour):
        self.make = make
        self.colour = colour
        self.numwheels = 4

    def display(self):
        print("Make:", self.make)
        print("Colour:", self.colour)
        print("Wheels:", self.numwheels)

#MainProgram
c0bj1 = Car("Ford","Black")
c0bj1.display()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Confusing in [for loop] topic Sherine 11 3,488 Jul-31-2021, 02:53 PM
Last Post: deanhystad
  Run the code for some stuff it does not return me why Anldra12 3 2,836 Apr-19-2021, 02:01 PM
Last Post: Anldra12
  So, a mass of errors trying to run a zipline-dependant program on 3.5 env Evalias123 2 2,395 Jan-21-2021, 02:22 AM
Last Post: Evalias123
  Unable to print stuff from while loop Nick1507 4 2,317 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  How Do I Install Stuff for Python? CopBlaster 6 3,178 May-08-2020, 12:27 PM
Last Post: hussainmujtaba
  Learning to have Class and doing stuff with it... bako 2 1,983 Apr-29-2020, 05:07 PM
Last Post: bako
  Newbie needs help with a simple program feynarun 3 2,255 Jan-15-2020, 01:17 PM
Last Post: DeaD_EyE
  Confusing logic Blob 4 2,397 Nov-18-2019, 03:26 AM
Last Post: Blob
  Automate the boring stuff : the tic tac toe game DJ_Qu 7 6,639 Apr-24-2019, 12:22 PM
Last Post: ichabod801
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,816 Dec-30-2018, 04:07 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020