Question

#Python Using a for loop, write a function called farmAnimals() that accepts a list of animals...

#Python

  1. Using a for loop, write a function called farmAnimals() that accepts a list of animals on a farm, identifies the typical farm animals, and returns a list of these typical farm animals on the farm without duplicates. For this function, your typical farm animals are cow, chicken, goat, pig, and sheep. Sample output;

>>> animalsOnFarm = ['cow', 'cow', 'cow', 'dog', 'cat', 'bird']

>>> farmAnimals(animalsOnFarm)

['cow']

>>> animalsOnFarm = ['pig', 'sheep', 'cow', 'cow', 'cow', 'dog', 'cat', 'bird']

>>> farmAnimals(animalsOnFarm)

['pig', 'sheep', 'cow']

>>>

Homework Answers

Answer #1

I have written the program using PYTHON PROGRAMMING LANGUAGE.

OUTPUT :

CODE :

#function definition for farmAnimals

def farmAnimals(animalsOnFarm):

farmanimals = ['cow','chicken','goat','pig','sheep']

output = []#declared the output variable

if('list' in str(type(animalsOnFarm)) and len(animalsOnFarm)!=0):

#iterating over the animalsOnFarm list

for animal in animalsOnFarm:

if(animal not in output and animal in farmanimals):

#adding to the output variable if animal is not existing in it before

output.append(animal)

else:

continue

return output#return output

else:

#in case if the length of input list is zero

return "Invalid Input!!!"

#defind the input list

input_list = ['cow', 'cow', 'cow', 'dog', 'cat', 'bird']

#printing the input list and output list on the console

print("\nINPUT :\n"+str(input_list))

print("\nOUTPUT : \n"+str(farmAnimals(input_list)))

Thanks..

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
In Python: Problem 5] Write a function that accepts a list as argument and returns a...
In Python: Problem 5] Write a function that accepts a list as argument and returns a list that contains: (1) The positive elements of the taken list (2) The negative elements of the taken list (3) The odd elements of the taken list (4) The even elements of the taken list
Write a python function called unique words that takes a phrase as an input string and...
Write a python function called unique words that takes a phrase as an input string and returns a list of the unique words found in that phrase. The words should be listed in alphabetical order.
Write a function that accepts a line of text and a single letter as input (case...
Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the first character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter. Example: Input text = "When the SUN rises at dawn, the chicken flies into the window." Input letter = "t" Output = "The letter t has appeared 3...
Write a Python function called sumNxN with three parameters. The first parameter is a nested list...
Write a Python function called sumNxN with three parameters. The first parameter is a nested list (matrix) representing a matrix of size N x N. The second parameter is the integer N, and the third is a list of N zeros. Modify the list of zeros so that each entry is the sum of the corresponding column. There is no return. Note: You may assume the sizes provided are all correct.
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
Python Write function words() that takes one input argument—a file name—and returns the list of actual...
Python Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
USING PYTHON do all the he problems using while loop , continue and break 1-This problem...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem provides practice using a while True loop.write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter.The function twoWords takes two parameters: an integer, length, that is the length of the first word and a character, firstLetter, that is the first letter of the...
Python As an exercise, use incremental development to write a function called hypotenuse that returns the...
Python As an exercise, use incremental development to write a function called hypotenuse that returns the length of the hypotenuse of a right triangle given the lengths of the other two legs as arguments. Record each stage of the development process as you go. After the final stage of development, print the output of hypotenuse(3, 4) and two other calls to hypotenuse with different arguments. Include all of the following: An explanation of each stage of development, including code and...
Python pls create a function called search_position. This function returns a list of 2 tuples and...
Python pls create a function called search_position. This function returns a list of 2 tuples and the number should be start highest number. The first index is the number, and second are list of 2 tuples that sorted by position in alphabetical order: The first index will be position and second index will be champion's name(This also should be sorted by alphabetical order). team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT