Python Forum
Troubles with classes, taken from a book - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Troubles with classes, taken from a book (/thread-3582.html)



Troubles with classes, taken from a book - sylas - Jun-05-2017

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()



RE: Troubles with classes, taken from a book - buran - Jun-05-2017

you have misspelled First Class in the second class definition
class SecondClass(FistClass):



RE: Troubles with classes, taken from a book - sylas - Jun-05-2017

Thank you Buran, Now it works well. There was a second error in line 25, you should read: print('Current value = "%s"' % self.data)