From the course: Getting Started with Python Object Oriented Programming: A Hands-On Approach

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Challenge: Getters and setters

Challenge: Getters and setters

(upbeat music) - [Presenter] Welcome to the challenge for this chapter. So the challenge is to write a setter method for a BankAccount class. What you need to do is to complete the set_balance method, which we'll show you in a moment. And if the new balance is positive or zero, you need to go ahead and update it. But if the new value is negative, you need to print an error message and don't update it. So open up setters_challenge.py in the start folder for 04_05, and you'll see that on lines 27 and 28, you have a function stub for set_balance. Now, currently this does nothing and we can see that it does nothing. We have a balance of 1,000 and we still have a balance of 1,000 after updating it on line 41. So that's not much good. What you need to do is you need to complete the function stub for set_balance so that it allows you to update values which are zero or positive. And if you try and enter a negative value, it gives you an error message saying, "The balance must be non-negative."

Contents