Python Forum

Full Version: Troubles with classes, taken from a book
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my question here:Hi all.The FirstClass alone works well. But when I add the SecondClass pyCharm gives me an error Here is the output,at first:
Output:
USING CLASS King Arthur 3.14159 New value
Error:
Traceback (most recent call last): File "/home/sylvain/PycharmProjects/Py3/file3.py", line 23, in <module> class SecondClass(FistClass): NameError: name 'FistClass' is not defined Process finished with exit code 1
# file.3.py
print("USING CLASS")
class FirstClass:
    def setdata(self, value):
        self.data = value

    def display(self):
        print(self.data)


x = FirstClass()
y = FirstClass()
x.setdata("King Arthur")
y.setdata(3.14159)
x.display()
y.display()
x.data = "New value"
x.display()

x.anothername = "spam"


class SecondClass(FistClass):
    def display(self):
        print('Current value = "%"' % self.data)


z = SecondClass()
z.setdata(42)
z.display()
my code here

Try to have tagged code:
# file.3.py
print("USING CLASS")
class FirstClass:
    def setdata(self, value):
        self.data = value

    def display(self):
        print(self.data)


x = FirstClass()
y = FirstClass()
x.setdata("King Arthur")
y.setdata(3.14159)
x.display()
y.display()
x.data = "New value"
x.display()

x.anothername = "spam"


class SecondClass(FistClass):
    def display(self):
        print('Current value = "%"' % self.data)


z = SecondClass()
z.setdata(42)
z.display()
you have misspelled First Class in the second class definition
class SecondClass(FistClass):
Thank you Buran, Now it works well. There was a second error in line 25, you should read: print('Current value = "%s"' % self.data)