-
Notifications
You must be signed in to change notification settings - Fork 89
Missing charge_phone() method in Your-Journey-To-Fluent-Python /15.Introduction-to-OOP.md #74
Copy link
Copy link
Open
Description
Here is the original code:
class Smartphone:
def init(self, brand, model):
self.brand = brand # Public attribute
self.model = model # Public attribute
self.__battery_level = 100 # Private attribute (initially fully charged)
self._installed_apps = [] # Protected attribute (initial app list)
def install_app(self, app_name):
"""Public method to install a new app."""
if app_name not in self._installed_apps:
self._installed_apps.append(app_name)
print(f"App '{app_name}' installed.")
else:
print(f"App '{app_name}' is already installed.")
def uninstall_app(self, app_name):
"""Public method to uninstall an app."""
if app_name in self._installed_apps:
self._installed_apps.remove(app_name)
print(f"App '{app_name}' uninstalled.")
else:
print(f"App '{app_name}' is not installed.")
def show_installed_apps(self):
"""Public method to display installed apps."""
print("Installed Apps:", self._installed_apps)
def __check_battery(self, required_amount):
"""Private method to check if enough battery is available."""
return self.__battery_level >= required_amount
def get_battery_level(self):
"""Public method to check the battery level."""
return f"Current battery level: {self.__battery_level}%"
Creating and interacting with a Smartphone object
my_phone = Smartphone('Pixel', 'Pixel 5')
Installing and uninstalling apps
my_phone.install_app('WhatsApp')
my_phone.install_app('Spotify')
my_phone.uninstall_app('WhatsApp')
my_phone.show_installed_apps()
Using battery and charging
print(my_phone.get_battery_level())
my_phone.charge_phone(30)
#updated part: function: charge_phone is not defined in the class. So the code generated error message when running. I added the part below, hope it would be helpful. Thanks for your sharing.
def charge_phone(self, amount): self.__batterry_level = min(100, self.__batterry_level + amount) print(f"Battery charged to {self.__batterry_level}%")
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels