蛋仔派对Python代码
时间: 2023-11-05 22:45:23 浏览: 382
当然,请见下方的蛋仔派对的 Python 代码示例:
```python
class Egg:
def __init__(self, color):
self.color = color
def display(self):
print(f"This egg is {self.color}.")
class EggParty:
def __init__(self, num_of_eggs):
self.eggs = [Egg("") for _ in range(num_of_eggs)]
def set_egg_color(self, egg_index, color):
self.eggs[egg_index].display()
self.eggs[egg_index] = Egg(color)
self.eggs[egg_index].display()
num_of_eggs = int(input("Enter the number of eggs: "))
party = EggParty(num_of_eggs)
for i in range(num_of_eggs):
color = input(f"Enter the color of egg {i+1}: ")
party.set_egg_color(i, color)
```
这个代码示例展示了一个简单的蛋仔派对的场景。用户需要输入蛋的数量,然后逐个输入每个蛋的颜色。每个蛋的颜色都会被显示出来,然后更改为用户输入的颜色,并再次显示出来。请根据您的实际需求进行修改和扩展。