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
#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


Messages In This Thread
Class code problem from CS Dojo YouTube - by Dixon - Feb-04-2020, 07:20 PM
RE: Class code problem from CS Dojo YouTube - by snippsat - Feb-04-2020, 10:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 765 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 430 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,255 Sep-26-2020, 10:54 PM
Last Post: SheeppOSU
  Practice problem using lambda inside the class jagasrik 3 2,312 Sep-12-2020, 03:18 PM
Last Post: deanhystad
  [split] Python Class Problem astral_travel 12 5,303 Apr-29-2020, 07:13 PM
Last Post: michael1789
  Python Class Problem JPCrosby 2 2,394 Apr-28-2020, 06:18 PM
Last Post: buran
  Class problem duckduck23 2 2,110 Feb-10-2020, 08:52 PM
Last Post: jefsummers
  Class Problem scratchmyhead 3 2,447 Nov-19-2019, 08:28 AM
Last Post: Larz60+
  problem with class method AmirAB 3 3,557 Feb-13-2019, 01:51 AM
Last Post: AmirAB
  A problem with child class Truman 2 2,891 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