Python Forum
Troubles with classes, taken from a book
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with classes, taken from a book
#1
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()


User has been warned for this post. Reason: code/error tags, meaningless title
Reply
#2
you have misspelled First Class in the second class definition
class SecondClass(FistClass):
Reply
#3
Thank you Buran, Now it works well. There was a second error in line 25, you should read: print('Current value = "%s"' % self.data)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  troubles with tqdm max22 2 486 Nov-27-2023, 09:20 PM
Last Post: max22
  Installation troubles on Win 10 peringek 1 2,887 Dec-31-2020, 07:30 AM
Last Post: caleb_cruze
  converting user input to float troubles RecklessTechGuy 3 2,406 Aug-17-2020, 12:41 PM
Last Post: deanhystad
  csv troubles DPaul 4 2,280 Aug-13-2020, 05:18 PM
Last Post: DPaul
  Win32API Troubles daaaabs 2 2,896 Mar-24-2020, 08:19 PM
Last Post: daaaabs
  local variable troubles yokaso 4 3,160 Oct-20-2019, 05:25 PM
Last Post: ichabod801
  Troubles with instaling pocketsphinx Thais781 1 2,602 Aug-07-2018, 10:05 AM
Last Post: Larz60+
  Using classes? Can I just use classes to structure code? muteboy 5 4,978 Nov-01-2017, 04:20 PM
Last Post: metulburr
  [?] UTF8, Unicode and Binary data reading troubles doublezero 1 3,134 Mar-31-2017, 11:32 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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