简单垃圾分类python代码
以下是一个简单的垃圾分类Python代码示例:
# 定义垃圾分类函数
def classify_garbage(garbage):
# 垃圾分类规则
recyclable = ['paper', 'plastic', 'metal', 'glass']
hazardous = ['battery', 'chemicals', 'medicine']
kitchen = ['food', 'fruit peel', 'vegetable peel']
other = ['clothes', 'shoes', 'electronics', 'furniture']
# 判断垃圾属于哪一类
if garbage in recyclable:
return 'recyclable'
elif garbage in hazardous:
return 'hazardous'
elif garbage in kitchen:
return 'kitchen'
elif garbage in other:
return 'other'
else:
return 'unknown'
# 测试垃圾分类函数
print(classify_garbage('paper')) # 输出:recyclable
print(classify_garbage('battery')) # 输出:hazardous
print(classify_garbage('banana peel')) # 输出:kitchen
print(classify_garbage('book')) # 输出:other
print(classify_garbage('unknown')) # 输出:unknown
该代码定义了一个垃圾分类函数,根据垃圾的种类将其归为可回收、有害、厨余和其他四类之一。通过调用该函数并传入垃圾名称,可以得到该垃圾属于哪一类。