You are on page 1of 2

# Smartphone class

class Smartphone:
# constructor for this class
# if name is not passed it is by default
# equal to None
def __init__(self, name=None) -> None:
# initialize features as dictionary
self.features = dict()
# initialize smartphone_name = name
self.smartphone_name = name

# addFeature function that receives name and value as parameter


# for example addFeature(name="Display", value="6.5 inch")
def addFeature(self, name, value):
# if smartphone name is not found give error message and return
if(self.smartphone_name==None):
print("Feature can not be added without phone name")
return
# else check if features name key is found or not
# in case it is not found set its value equal to []
self.features.setdefault(name, [])

# add value to the list


self.features[name].append(value)

# setName function that receives smartphone_name as argument


def setName(self, name):
# set simply set smartphone name equal to name
self.smartphone_name = name

# printDetail function
def printDetail(self):
# check if smartphone_name is found or not
# in case it is none give error message and return
if(self.smartphone_name==None):
print("Cannot display phone info without phone name")
return

# else display phone name


print("Phone Name:",self.smartphone_name)

# and loop over features dictionary key and value


for key, value in self.features.items():
# print key:
print(key+": ", end="")
# not print list of elements in value one by one
for i in range(len(value)):
# if last element we don't print , (comma)
if(i==len(value)-1):
print(value[i])
# else print , (comma) and end="" for this case
else:
print(value[i]+", ",end="")

# Do not change the following lines of code.


s1 = Smartphone()
print("=================================")
s1.addFeature("Display", "6.1 inch")
print("=================================")
s1.setName("Samsung Note 20")
s1.addFeature("Display", "6.1 inch")
s1.printDetail()
print("=================================")
s2 = Smartphone("Iphone 12 Pro")
s2.addFeature("Display", "6.2 inch")
s2.addFeature("Ram", "6 GB")
print("=================================")
s2.printDetail()
s2.addFeature("Display", "Amoled panel")
s2.addFeature("Ram", "DDR5")
print("=================================")
s2.printDetail()
print("=================================")

You might also like