0

Please complete this code.

class Dog: def __init__(self,name): self.name="name" dog=Dog def__str__(self): #What should I write here?

3rd Jul 2025, 8:38 AM
Ali
Ali - avatar
2 odpowiedzi
+ 3
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): ...
3rd Jul 2025, 1:14 PM
Lothar
Lothar - avatar
+ 2
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
3rd Jul 2025, 10:10 AM
Sawa Abraham
Sawa Abraham - avatar