python三只小猪称体重
时间: 2024-01-26 13:14:27 浏览: 143
python绘制小猪佩奇py代码_python代码_python_小猪佩奇python_transportation9lf_
以下是一个示例代码,用于演示如何使用Python来表示和比较三只小猪的体重:
```python
class Pig:
def __init__(self, name, weight):
self.name = name
self.weight = weight
# 创建三只小猪的实例
pig1 = Pig("小猪1", 50)
pig2 = Pig("小猪2", 60)
pig3 = Pig("小猪3", 70)
# 比较三只小猪的体重
if pig1.weight > pig2.weight and pig1.weight > pig3.weight:
print(pig1.name, "体重最大")
elif pig2.weight > pig1.weight and pig2.weight > pig3.weight:
print(pig2.name, "体重最大")
else:
print(pig3.name, "体重最大")
```
输出结果将是体重最大的小猪的名字。
阅读全文