My code is throwing this TypeError: __str__ returned non-string (type NoneType), Please tell how to resolve! | Sololearn: Learn to code for FREE!
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)

27th Dec 2018, 10:20 AM
Aakash Gupta
Aakash Gupta - avatar
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
27th Dec 2018, 10:31 AM
Taste
Taste - avatar
+ 1
Taste is correct. __str___ method should return a string instead of that print statement
27th Dec 2018, 10:50 AM
Шащи Ранжан
Шащи Ранжан - avatar
0
Ok thanks to you both
27th Dec 2018, 2:38 PM
Aakash Gupta
Aakash Gupta - avatar