用Python帮我写一个垃圾分类的程序
时间: 2023-05-12 16:04:18 浏览: 176
当然,我很乐意帮您写一个垃圾分类的程序。以下是一个简单的 Python 代码示例,可以根据垃圾的类型将其分类为可回收垃圾、有害垃圾、厨余垃圾或其他垃圾:
```
def classify_garbage(garbage):
recyclable = ["paper", "plastic", "metal", "glass"]
hazardous = ["battery", "chemicals", "paint", "medicine"]
kitchen = ["food waste", "fruit peels", "vegetable scraps"]
if garbage in recyclable:
return "recyclable"
elif garbage in hazardous:
return "hazardous"
elif garbage in kitchen:
return "kitchen waste"
else:
return "other waste"
```
您可以使用这个函数来分类您的垃圾,例如:
```
>>> classify_garbage("plastic")
'recyclable'
>>> classify_garbage("battery")
'hazardous'
>>> classify_garbage("banana peel")
'kitchen waste'
>>> classify_garbage("old clothes")
'other waste'
```
希望这个程序能够帮助您更好地分类垃圾,保护环境。
阅读全文