蚂蚁王国python代码
时间: 2023-11-09 08:02:44 浏览: 113
兰顿蚂蚁源代码python3
以下是一个简单的蚂蚁王国的 Python 代码示例:
```python
class Ant:
def __init__(self, name, role):
self.name = name
self.role = role
class Colony:
def __init__(self, name):
self.name = name
self.ants = []
def add_ant(self, ant):
self.ants.append(ant)
def remove_ant(self, ant):
self.ants.remove(ant)
# 创建一个蚂蚁王国
colony = Colony("Ant Kingdom")
# 添加一些蚂蚁
ant1 = Ant("Ant 1", "Worker")
ant2 = Ant("Ant 2", "Soldier")
colony.add_ant(ant1)
colony.add_ant(ant2)
# 移除一个蚂蚁
colony.remove_ant(ant1)
# 输出当前蚂蚁王国的信息
print(colony.name)
for ant in colony.ants:
print(ant.name, ant.role)
```
输出结果:
```
Ant Kingdom
Ant 2 Soldier
```
阅读全文