正方形面积python
时间: 2024-01-16 16:18:49 浏览: 36
以下是两种计算正方形面积的方法:
1. 使用函数计算正方形面积:
```python
def calculate_square_area(side_length):
return side_length ** 2
side_length = 5
area = calculate_square_area(side_length)
print("The area of the square is:", area) # 输出:The area of the square is: 25
```
2. 使用面积计算器计算正方形面积:
```python
class AreaCalculator:
def __init__(self, shape):
self.shape = shape
def calculate_area(self):
if self.shape == "square":
side_length = float(input("Enter the side length of the square: "))
area = side_length ** 2
print("The area of the square is:", area)
else:
print("Invalid shape.")
shape = "square"
calculator = AreaCalculator(shape)
calculator.calculate_area()
```