In Python classifying animals as mammals, birds, fish, reptiles, or amphibian. Y
ID: 3849476 • Letter: I
Question
In Python
classifying animals as mammals, birds, fish, reptiles, or amphibian. You will classify these animals by asking the user “which of the following features does the animal have: fur, feathers, scales, or skin?” If the user inputs “fur”, “feathers” or “scales”, then you will respond with “mammal”, “bird”, or “fish” respectively. If the user inputs skin, then you will additionally prompt them with “is the skin dry or moist”. If the user enters “dry” then you should return “reptile”, if they enter “moist” then you should return “amphibian”. If at any time the user enters an invalid response, you should print “invalid input” and the program should end. You will use an if/elif/else tree to do these classifications, and your final classification tree will look like this.
Explanation / Answer
Code:
#!/usr/bin/python
def main():
# taking input on the features from user
features = raw_input('which of the following features does the animal have: fur, feathers, scales, or skin? ')
# converting to lowercase so we can compare with lower case values easily
features = features.lower()
# doing a decision making using if
if features == 'fur':
print('The animal is Mammal')
elif features == 'feathers':
print('The animal is bird')
elif features == 'scales':
print('The animal is fish')
elif features == 'skin':
# if features provided is skin we will ask for additional input like the skin dry or moist
stype = raw_input('is the skin dry or moist? ')
stype = stype.lower()
if stype == 'dry':
print('The animal is reptile')
elif stype == 'moist':
print('The animal is amphibian')
# if user enters wrong skin type
else:
print('Wrong skin type provided. Available options are "dry" or "Moist"')
# if user enters wrong feature
else:
print('Wrong features provided. Available options are "fur" "feathers" or "scales"')
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> vi iftest.py
Unix Terminal> python iftest.py
which of the following features does the animal have: fur, feathers, scales, or skin? fur
The animal is Mammal
Unix Terminal> python iftest.py
which of the following features does the animal have: fur, feathers, scales, or skin? feathers
The animal is bird
Unix Terminal> python iftest.py
which of the following features does the animal have: fur, feathers, scales, or skin? scales
The animal is fish
Unix Terminal> python iftest.py
which of the following features does the animal have: fur, feathers, scales, or skin? skin
is the skin dry or moist? moist
The animal is amphibian
Unix Terminal> python iftest.py
which of the following features does the animal have: fur, feathers, scales, or skin? skin
is the skin dry or moist? moist
The animal is amphibian
Unix Terminal> python iftest.py
which of the following features does the animal have: fur, feathers, scales, or skin? skin
is the skin dry or moist? dry
The animal is reptile
Unix Terminal> python iftest.py
which of the following features does the animal have: fur, feathers, scales, or skin? hello
Wrong features provided. Available options are "fur" "feathers" or "scales"
Unix Terminal> python iftest.py
which of the following features does the animal have: fur, feathers, scales, or skin? skin
is the skin dry or moist? hello
Wrong skin type provided. Available options are "dry" or "Moist"
Unix Terminal>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.