Python Practice › OOP Programs › Program 42
Phone class
Create Phone with brand, model and storage, with methods to call, text and check storage.
Output
Calling 98765 43210 from Galaxy A55 Text to 98765 43210: Running late 81 GB free
Approach
- Track used storage and report what is free.
Solution
class Phone: def __init__(self, brand, model, storage_gb): self.brand, self.model, self.storage_gb = brand, model, storage_gb self.used_gb = 0 def call(self, number): return f"Calling {number} from {self.model}" def text(self, number, message): return f"Text to {number}: {message}" def free_storage(self): return self.storage_gb - self.used_gb p = Phone("Samsung", "Galaxy A55", 128)p.used_gb = 47print(p.call("98765 43210"))print(p.text("98765 43210", "Running late"))print(p.free_storage(), "GB free")Output
Calling 98765 43210 from Galaxy A55 Text to 98765 43210: Running late 81 GB free
Learn the concept
New to this topic? Read the lesson, then come back.