K K
K K

Reputation: 41

How to add units from a funtion to main lists

I have a problem where I need to classify species found in a forest to different lists. Here is my code so far:

# we assume these to be mutually exclusive lists
TREES = ["spruce", "pine", "birch", "maple", "willow", "oak"]
MUSHROOMS = ["false morel", "chantarelle", "milkcap", "funnel chantarelle", "brittlegill", "black trumpet", "forest lamb"]
FLOWERS = ["lily", "bluebell", "violet", "daisy", "red clover", "dandelion", "yarrow", "anemone"]

OTHER = 0
TREE = 1
MUSHROOM = 2
FLOWER = 3


def print_in_alphabetical_order(list_of_strings):
    list_of_strings.sort()
    for i in list_of_strings:
        print(i)

def classify_species(species):
    i = str(species)
    if i in TREES:
        return TREE
    elif i in MUSHROOMS:
        return MUSHROOM
    elif i in FLOWERS:
        return FLOWER
    else:
        return OTHER

def main():
    species = []
    lajike = str(input("Enter the species of the items you found in the forest. Stop with empty line.\n"))
    while lajike:
        species.append(lajike)
        lajike = str(input())
        end = "\n"
    print("Your finds in alphabetical order:")
    print_in_alphabetical_order(species)
    trees = []
    mushrooms = []
    flowers = []
    other = []

The "classify_species" function should be working fine. The problem is, how do I add the items from [species] list to the other lists in main function? They should be added using the "classify_species" function. I assume it has to be done with for-command but I don't know why. Thanks a lot for help!

Upvotes: 0

Views: 43

Answers (1)

BluTen
BluTen

Reputation: 65

I Assume you are asking how to filter the items from species list to all the other lists trees, mushrooms, flowers, other

# we assume these to be mutually exclusive lists
TREES = ["spruce", "pine", "birch", "maple", "willow", "oak"]
MUSHROOMS = ["false morel", "chantarelle", "milkcap", "funnel chantarelle", "brittlegill", "black trumpet", "forest lamb"]
FLOWERS = ["lily", "bluebell", "violet", "daisy", "red clover", "dandelion", "yarrow", "anemone"]

OTHER = 0
TREE = 1
MUSHROOM = 2
FLOWER = 3


def print_in_alphabetical_order(list_of_strings):
    list_of_strings.sort()
    for i in list_of_strings:
        print(i)

def classify_species(species):
    i = str(species)
    if i in TREES:
        return TREE
    elif i in MUSHROOMS:
        return MUSHROOM
    elif i in FLOWERS:
        return FLOWER
    else:
        return OTHER

def main():
    species = []
    lajike = str(input("Enter the species of the items you found in the forest. Stop with empty line.\n"))
    while lajike:
        species.append(lajike)
        lajike = str(input())
        end = "\n"
    print("Your finds in alphabetical order:")
    print_in_alphabetical_order(species)
    
    trees = []
    mushrooms = []
    flowers = []
    other = []

    for i in species:
        result = classify_species(i)
        if result == TREE:
            trees.append(i)
        elif result == MUSHROOM:
            mushrooms.append(i)
        elif result == FLOWER:
            flowers.append(i)
        else:
            other.append(i)

Upvotes: 1

Related Questions