0
Please complete this code.
class Dog: def __init__(self,name): self.name="name" dog=Dog def__str__(self): #What should I write here?
3 Antwoorden
+ 7
abas , Sawa Abraham ,
just saying `... please complete this code ...` is not enough information what the code should achieve. can you update your post ?
there are also 2 issues that prevents the code from running properly:
> ...
self.name = "name" does not assign the parameter that is passed to the class instance but just assigns the string `name`. should be:
self.name = name
...
>...
def__str__(self): missing space after `def`, the method that should do the output should be defined as:
def __str_(self):
...
+ 5
class Dog:
def __init__(self,name):
self.name= name
def __str__(self):
'''you can put a string that will displayed any the print function in used on the class instance'''
return f"{self.name} is my dog"
dog = Dog("max")
print(dog)
# output: max is my dog
+ 2
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name} is {self.age} years old and a good doggo 🐕"
dog = Dog("Bruno", 3)
print(dog)