This is a Python programming I got these codes. Please assist me answering quest
ID: 3694537 • Letter: T
Question
This is a Python programming I got these codes. Please assist me answering question right blew codes.
class insect:
_legs = 6
def __init__(self, name):
self._name = name
def getName(self):
return self._name
def setName(self, name):
self._name = name
def getLegs(self):
return self._legs
def main():
ladybug = insect("Ladybug")
number_of_legs = ladybug.getLegs()
print(ladybug.getName() + " has " )
print(str(number_of_legs) + " legs"))
main()
Question
Modify the program on the previous slide to create a butterfly insect object
Modify the class code so that the insect objects also store their color
And the number of wings they have (A butterfly has 4, an ant has 0, a ladybug has 2)
Now, add a __str__ method to print out all the information
about an insect object
Create an ant object
Now use your __str__ method in main to print out all the info about your butterfly, ant and ladybug objects.
Explanation / Answer
Here is the code for you:
#!/usr/bin/python
class insect:
#Modify the class code so that the insect objects also store their color
_legs = 6
_color = ""
#And the number of wings they have (A butterfly has 4, an ant has 0, a ladybug has 2)
_wings = 0
def __init__(self, name):
self._name = name
def getName(self):
return self._name
def setName(self, name):
self._name = name
def getLegs(self):
return self._legs
def setWings(self, wings):
self._wings = wings
def setColor(self, color):
self._color = color
#Now, add a __str__ method to print out all the information about an insect object
def __str__(self):
output = "Name = ", self._name,
output = output, ' Legs = ', self._legs,
output = output, ' Color = ', self._color,
output = output, 'Wings = ', self._wings
def main():
ladybug = insect("Ladybug")
ladybug.setWings(2)
number_of_legs = ladybug.getLegs()
print ladybug.getName() + " has ",
print str(number_of_legs) + " legs"
#Modify the program on the previous slide to create a butterfly insect object
butterfly = insect("butterfly")
butterfly.setWings(4)
ant = insect("ant")
ant.setColor("black")
ant.setWings(0)
print ant.str()
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.