50.2k views
4 votes
Fill in the blanks to complete the “endangered_animals” function. This function accepts a dictionary containing a list of endangered animals (keys) and their remaining population (values). For each key in the given “animal_dict” dictionary, format a string to print the name of the animal, with one animal name per line.

def endangered_animals(animal_dict):

result = ""

# Complete the for loop to iterate through the key and value items

# in the dictionary.

for ___

# Use a string method to format the required string.

result += ___

return result



print(endangered_animals({"Javan Rhinoceros":60, "Vaquita":10, "Mountain Gorilla":200, "Tiger":500}))

# Should print:

# Javan Rhinoceros

# Vaquita

# Mountain Gorilla

# Tiger

User Srneczek
by
7.8k points

1 Answer

4 votes

Answer:

def endangered_animals(animal_dict):

result = ""

for animal, population in animal_dict.items():

result += f"{animal}\\"

return result

print(endangered_animals({"Javan Rhinoceros":60, "Vaquita":10, "Mountain Gorilla":200, "Tiger":500}))

Step-by-step explanation:

The endangered_animals function accepts a dictionary containing a list of endangered animals (keys) and their remaining population (values). It uses a for loop to iterate through the dictionary items and format a string containing the name of each animal with a newline character at the end. Finally, the function returns the resulting string.

When this function is called with the example dictionary {"Javan Rhinoceros":60, "Vaquita":10, "Mountain Gorilla":200, "Tiger":500}, it prints the following output:

Javan Rhinoceros

Vaquita

Mountain Gorilla

Tiger

User LifeLongStudent
by
8.4k points

No related questions found