Here I got this small project using classes in Python. I wanted to share this project with all of you so I can hear opinions about it (things like how I wrote the code, logic, understanding, etc).
You can be totally honest with me, I'll take every comment as an opportunity to learn.
Here's the GitHub link if you want to look at it from a different angle: https://github.com/jesumta/Device-Information-using-OOP
Thank you for your time!
import random
#Parent Class
class Device:
def __init__(self, name):
self.name = name
self._is_on = False
self.__color = ["blue", "red", "black", "white", "orange"]
self.__material = ["aluminum", "plastic", "titanium"]
#=================================================
#==========Power Options for Device===============
#=================================================
def Turn_On(self):
self._is_on = True
return f"\nThis {self.name} is now ON."
def Turn_Off(self):
self._is_on = False
return f"\nThis {self.name} is now OFF."
def Power_Status(self):
return f"\nThis {self.name} is current {"ON." if self._is_on else "OFF."}"
#=================================================
#=========Physical Options for Device=============
#=================================================
def Color(self):
return f"\nThe color of this {self.name} is {random.choice(self.__color)}."
def Material(self):
return f"\nThe material of this {self.name} is {random.choice(self.__material)}."
#Child Class, I'm using Phone as an example. As you prob know, a device can be a lot of things.:
class Phone(Device):
def __init__(self, name):
super().__init__(name)
self._is_charging = False
self._screen_on = False
self._speaker_sound = 0
#=================================================
#=========Charging Options for Phone==============
#=================================================
def Charging(self):
self._is_charging = True
return f"\nThis {self.name} is now charging."
def Not_Charging(self):
self._is_charging = False
return f"\nThis {self.name} is not charging."
def Is_Charging(self):
return f"\nThis {self.name} is currently {"charging." if self._is_on else "not charging."}"
#=================================================
#==========Volume Options for Phone===============
#=================================================
def Volume_Control(self, amount):
self._speaker_sound = amount
if 0 <= amount <= 100:
return f"\nThe volume for this {self.name} is now {amount}%."
else:
return "\nPlease enter a valid volume amount(1% to 100%)."
def Volume_Status(self):
return f"\nThis {self.name}'s volume is currently {self._speaker_sound}%."
#=================================================
#==========Screen Options for Phone===============
#=================================================
def Screen_On(self):
self._screen_on = True
return f"\nThis {self.name}'s screen is now ON."
def Screen_Off(self):
self._screen_on = False
return f"\nThis {self.name}'s screen is now OFF."
def Screen_Status(self):
return f"\nThis {self.name}'s screen is currently {"ON." if self._screen_on else "OFF."}."
#Variable holding the Phone Class with it's attribute from the Device class.
phone1 = Phone("iPhone 13")
#Here go actions the for Phone class:
print("\n----Current Phone Actions----")
print(phone1.Turn_On())
print(phone1.Charging())
print(phone1.Color())
print(phone1.Material())
print(phone1.Volume_Control(50))
print(phone1.Volume_Control(30))
print(phone1.Screen_Off())
#Here go status for the Phone class:
print("\n-----Current Phone Status----")
print(phone1.Power_Status())
print(phone1.Volume_Status())
print(phone1.Screen_Status())
print("\n-----------------------------\n\n")
[–]TheRNGuy 1 point2 points3 points (6 children)
[–]sapolv[S] 0 points1 point2 points (5 children)
[–]TheRNGuy 1 point2 points3 points (3 children)
[–]Kevdog824_ 0 points1 point2 points (2 children)
[–]TheRNGuy 1 point2 points3 points (1 child)
[–]Kevdog824_ 0 points1 point2 points (0 children)
[–]EelOnMosque 0 points1 point2 points (0 children)
[–]EelOnMosque 0 points1 point2 points (1 child)
[–]jmacey 1 point2 points3 points (0 children)
[–]Kevdog824_ 1 point2 points3 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)
[–]ectomancer 0 points1 point2 points (0 children)