New course! Every coder should learn Generative AI!
Try a free lesson+ 2
My code is throwing this TypeError: __str__ returned non-string (type NoneType), Please tell how to resolve!
class Account: def __init__(self, owner, balance): self.owner = owner self.balance = balance def __str__(self): print("Account owner: {}".format(self.owner)) print("Account balance: {}".format(self.balance)) acct1 = Account("Joe",300) print(acct1)
3 Answers
+ 4
I think __str__ should return a string value instead of print a string value
def __str__(self){
return "Account owner : "+self.owner+"\nAccount balance : "+self.balance
}
it could be wrong, i'm not so good with python
+ 1
Taste is correct. __str___ method should return a string instead of that print statement
0
Ok thanks to you both