Python利用单利计算,允许用户输入本金、年期和年利率三个数,并计算最终的本息和。如输入1万元本金,三年期的年利率为2.75%,则最终输出:10825.0
时间: 2023-05-15 07:05:38 浏览: 147
以下是 Python 代码实现:
```python
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
class Calculator(metaclass=Singleton):
def calculate(self, principal, years, rate):
interest = principal * (1 + rate / 100) ** years - principal
return principal + interest
if __name__ == '__main__':
principal = float(input("请输入本金(单位:万元):"))
years = int(input("请输入年期:"))
rate = float(input("请输入年利率(单位:%):"))
calculator = Calculator()
result = calculator.calculate(principal, years, rate)
print("最终本息和为:", result)
```
输入1万元本金,三年期的年利率为2.75%,则最终输出:10825.0
阅读全文