Question: Step 3 . Create new module named person.py Follow these step - by - step instructions to create a Python class named Person within the

Step 3. Create new module named person.py
Follow these step-by-step instructions to create a Python class named Person within the person.py module. The
person class has private variables for first name, last name, and the amount of money. Youll also write a
function to update the money amount with safeguards to prevent the balance from going negative. Additionally,
youll write a simple function to display the persons information.
Step 1: Define the Person Class and the Constructor
Create a class named Person.
Inside the class, define a method called __init__. This is the constructor method that gets called
whenever you create a new Person object.
The __init__ method should take three parameters:
first_name: the first name of the person.
last_name: the last name of the person.
amount_of_money: the amount of money the person has (should be a floating-point number).
I
inside the constructor:
Store the first_name and last_name as private variables (use double underscores like __first_name and
__last_name).
Store the amount_of_money as a private variable (__amount_of_money), and ensure it is converted to a
f
loat using float(amount_of_money).
Step 2: Write Getters and Setters for First Name and Last Name
Getters are functions that allow you to access private variables from outside the class.
Setters are functions that allow you to modify the value of private variables, but only if the new value is
valid.
For first_name:
Write a get_first_name() method that returns the value of __first_name.
Write a set_first_name(first_name) method to change the first name. Ensure that the new first name is
not empty by adding a check (raise a ValueError if its empty).
For last_name:
Write a get_last_name() method that returns the value of __last_name.
Write a set_last_name(last_name) method to change the last name, with a similar check to ensure its
not empty.
Step 3: Write a Getter for the Amount of Money
Write a get_amount_of_money() method to return the value of __amount_of_money. This will allow you
to check the persons balance later on.
Step 4: Write the update_money_value() Method
Create a method called update_money_value(amount) that takes a number (positive or negative).
This method will add the value of amount to __amount_of_money.
However, if the resulting balance would go below 0, the method should print a warning and not allow the
update. Use the if statement to check whether adding amount would result in a negative balance.
Step 5: Write the displayInfo() Method
Create a displayInfo() method that prints out the persons full name and current balance in a readable format.
name: Albert Einstein money: $60.0
This is the output for a person whose first name is Albert, last name is Einstein and has $60.00
Step 6: Write the main() Function
Write a main() function that creates a Person object, displays the persons info, performs a few
transactions, and displays the updated info after each transaction.
Test the update_money_value() method by attempting to withdraw more money than the person has and
ensure it shows a warning.
def main()-> None:
test_person = Person("Albert", "Einstein", 100.00)
test_person.displayInfo()
test_person.update_money_value(-90)
test_person.displayInfo()
test_person.update_money_value(50)
test_person.displayInfo()
test_person.update_money_value(-100)
test_person.displayInfo()
Step 7: Ensure the Program Runs
Add the standard Python entry point to run your code:
Output for running code:
name: Albert Einstein money: $ 100.00
name: Albert Einstein money: $ 10.00
name: Albert Einstein money: $ 60.00
Transaction cannot be done due to lack of funds.
name: Albert Einstein money: $ 60.00

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!