logo CodeStepByStep logo


Language/Type: Python classes
Related Links:

Add a __str__ method to the BankAccount class. Use your BankAccount class solution from the previous exercises as a starting point to solve this exercise.

Suppose that a class of objects named BankAccount has already been created that remembers information about a user's account at a bank. The class includes the following public members:

member name type description
BankAccount(name) constructor constructs a new account for the person with the given name, with $0.00 balance
data attributes private attributes storing account's name and balance
ba.name property the account name as a string (read-only)
ba.balance property the account balance as a real number (read-only)
ba.deposit(amount) method adds the given amount of money, as a real number, to the account balance; if the amount is negative, does nothing
ba.withdraw(amount) method subtracts the given amount of money, as a real number, from the account balance; if the amount is negative or exceeds the account's balance, does nothing

Add a __str__ method to the class that returns a string containing the account's name and balance separated by a comma and space. Round the balance to two digits after the decimal point. For example, if an account object named yana has the name "Mariana" and a balance of 3.5 (representing $3.50), the call str(yana) should return the string "Mariana, $3.50". Submit only your __str__ method, not the complete class.

Member function: Write a member function that will become part of an existing class. You do not need to write the complete class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.