Python Forum
Class code problem from CS Dojo YouTube
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class code problem from CS Dojo YouTube
#1
class Robot:
def introduce_self(self)
print("My name is " + self.name)

r1 = Robot()
r1.name = "Tom"
r1.color = "red"
r1.weight = 30

r2 = Robot()
r2.name = "Jerry"
r2.color = "blue"
r2.weight = 40

r1.introduce_self()
r2.introduce_self()
With the above code, copied from the YouTube channel of CS Dojo, I get a traceback
"name 'r1' is not defined.
Reply
#2
indentation is incorrect
Please supply URL
Reply
#3
https://www.youtube.com/watch?v=wfcWRAxRVBA&t=45s

I typed in the code and didn't do the indents. I'm rather new to this forum, and didn't realize that using the python thingy wouldn't do the job. Anyway, when it's properly indented in Jupyter Notebook, it throws a Traceback.

Never mind. I didn't refresh a block of code after making changes, thus giving me the "name not found" error.
Reply
#4
Can copy code under from YouTube link.
class Robot:
    def __init__(self, name, color, weight):
        self.name = name
        self.color = color
        self.weight = weight

    def introduce_self(self):
        print(f"My name is " + self.name)

# r1 = Robot()
# r1.name = "Tom"
# r1.color = "red"
# r1.weight = 30
#
# r2 = Robot()
# r2.name = "Jerry"
# r2.color = "blue"
# r2.weight = 40


r1 = Robot("Tom", "red", 30)
r2 = Robot("Jerry", "blue", 40)

r1.introduce_self()
r2.introduce_self()
Output:
My name is Tom My name is Jerry
So it's working.

Code over do i think is part-2,as you see some code is comment out.
To use comment out code it would be like this,also trow i in f-string.
class Robot:
    def introduce_self(self):
        print("My name is {self.name}")
Use:
>>> r1 = Robot()
>>> r1.name = "Tom"
>>> r1.color = "red"
>>> r1.name
'Tom'
>>> r1.introduce_self()
My name is Tom
So here is manually making the __init__ method in part 2.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 4 168 3 hours ago
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 243 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Class function is not running at all even though it is clearly run in the code SheeppOSU 2 2,087 Sep-26-2020, 10:54 PM
Last Post: SheeppOSU
  Practice problem using lambda inside the class jagasrik 3 2,159 Sep-12-2020, 03:18 PM
Last Post: deanhystad
  [split] Python Class Problem astral_travel 12 4,976 Apr-29-2020, 07:13 PM
Last Post: michael1789
  Python Class Problem JPCrosby 2 2,291 Apr-28-2020, 06:18 PM
Last Post: buran
  Class problem duckduck23 2 2,019 Feb-10-2020, 08:52 PM
Last Post: jefsummers
  Class Problem scratchmyhead 3 2,320 Nov-19-2019, 08:28 AM
Last Post: Larz60+
  problem with class method AmirAB 3 3,380 Feb-13-2019, 01:51 AM
Last Post: AmirAB
  A problem with child class Truman 2 2,783 Jul-02-2018, 12:37 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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