0% found this document useful (0 votes)
48 views2 pages

New Text Document 3

The Smartphone class defines features as a dictionary and allows adding features with name and value as parameters. The class can set a smartphone name and print details, checking for a name before printing and listing features by name and value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

New Text Document 3

The Smartphone class defines features as a dictionary and allows adding features with name and value as parameters. The class can set a smartphone name and print details, checking for a name before printing and listing features by name and value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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