EL/LAK - Python Course

worksheet 07

  1. What is the difference between a class and an object?
  2. What is the difference between a function and a method?
  3. Write code to create an instance of this class and set its attributes. Remember, don't store numbers as strings. Use 40 and not "40".
    class Dog():
    def __init__(self):
    self.age = 0
    self.name = ""
    self.weight = 0
  4. Write code to create two different instances of this class and set attributes for both objects. While a phone number is a number, those should be stored as strings. So we can keep leading zeros and those dashes.
    class Person():
    def __init__(self):
    self.name = ""
    self.cell_phone = ""
    self.email = ""
  5. For the code below, write a class that has the appropriate class name and attributes that will allow the code to work.
    my_bird = Bird()
    my_bird.color = "green"
    my_bird.name = "Sunny"
    my_bird.breed = "Sun Conure"
  6. Define a class that would represent a character in a simple 2D game. Include attributes for the position, name, and strength.
  7. The following code runs, but it is not correct. What did the programmer do wrong?
    class Person():
    def __init__(self):
    self.name = ""
    self.money = 0
    nancy = Person()
    name = "Nancy"
    money = 100
  8. Take a look at the code. It does not run. What is the error that prevents it from running?
    class Person():
    def __init__(self):
    self.name = ""
    self.money = 0
    bob = Person()
    print(bob.name, "has", money, "euros.")
  9. Even with that error fixed, the program will not print out:
    Bob has 0 euros.
    Instead it just prints out:
    has 0 euros.
    Why is this the case?