given is a code snippet in python : import sys class mammal : def _init_(self) :
ID: 3732639 • Letter: G
Question
given is a code snippet in python :
import sys
class mammal :
def _init_(self) :
self.legs = 4
self.face = (2,2,1)
def speak (self) :
pass
class dog (mammal) :
def speak (self) :
print "I bark"
class cat (mammal) :
def speak (self) :
print "I purr"
class pig (mammal) :
def speak (self) :
print "I grunt"
class tiger (cat) :
def speak (self) :
print "I roar"
class man (dog,cat,pig,tiger) :
pass
___________________________
Question - modify the code such that man will have 2 ears, 2 eyes, 1 nose, 2 legs, 2 hands and is able to speak like other mammals as well as should talk, i.e,
Man should speak :
I bark
I purr
I grunt
I roar
I even talk.
PLEASE INCLUDE ALL THE STEPS AND EXPLAIN THE ANSWER IN DETAIL
Explanation / Answer
import sys
class mammal :
def _init_(self) :
# 2 ears, 2 eyes, 1 nose, 2 legs, 2 hands
self.legs = 4
self.hands= 2
self.face = (2,2,1)
def speak (self) :
pass
class dog (mammal) :
def speak (self) :
print ("I bark")
#"super" pythonic way
super().speak()
class cat (mammal) :
def speak (self) :
print ("I purr")
#"super" pythonic way
super().speak()
class pig (mammal) :
def speak (self) :
print ("I grunt")
#"super" pythonic way
super().speak()
#change it to mammal since cat will be called twice while calling man class
#if you want to use tiger(cat), change man class to(dog,pig,tiger) ...hence, sequence of output will be changed.
class tiger (mammal) :
def speak (self) :
print ("I roar")
#"super" pythonic way
super().speak()
class man (dog,cat,pig,tiger) :
def speak (self) :
print ("Man is saying: ")
super().speak()
print ("I even talk")
m=man()
m.speak()
Important notes:
1.You use tiger(mammal) with man(dog,cat,pig,tiger):::
output:
Man is saying:
I bark
I purr
I grunt
I roar
I even talk
2.if you want to use tiger(cat), change man class to(dog,pig,tiger) ...hence, sequence of output will be changed.
Output:
Man is saying:
I bark
I grunt
I roar
I purr
I even talk
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.