Skip to the content.

Waste Sorter

AP CSP Project

def sort_waste(correct_items):
    waste = []
    for item in correct_items:
        if item in ['plastic bottle', 'can', 'paper', 'cardboard']:
            waste.append(item + " ➔ Recycle")
        elif item in ['banana peel', 'apple core', 'coffee grounds']:
            waste.append(item + " ➔ Compost")
        elif item in ['chip bag', 'styrofoam', 'plastic straw']:
            waste.append(item + " ➔ Trash")
    return " | ".join(waste)

def check_input(user_input):
    known_items = ['plastic bottle', 'can', 'paper', 'cardboard', 
                   'banana peel', 'apple core', 'coffee grounds',
                   'chip bag', 'styrofoam', 'plastic straw']
    
    good_items = []
    bad_items = []
    
    for item in user_input:
        if item in known_items:
            good_items.append(item)
        else:
            bad_items.append(item)
    return good_items, bad_items

if __name__ == "__main__":
    # Prompt user
    user_response = input("Enter items you are throwing away (separate with commas):\n")
    
    waste_input = [item.strip().lower() for item in user_response.split(',')]

    # Check the input
    good_items, bad_items = check_input(waste_input)

    # Output results
    if good_items:
        print("\nCorrectly recognized items and their sorting:\n", sort_waste(good_items))
    if bad_items:
        print('\nUnrecognized items:', ", ".join(bad_items))
        print('Number of unrecognized items:', len(bad_items))
Correctly recognized items and their sorting:
 plastic bottle ➔ Recycle | chip bag ➔ Trash | banana peel ➔ Compost

Unrecognized items: sock, chair
Number of unrecognized items: 2