《python语言程序设计》第5章 第24题 一道展示摊销时间的循环题 前面还有20984人

发布于:2023-02-01 ⋅ 阅读:(698) ⋅ 点赞:(0)

题目内容

我在解题过程中只看了注意的部分。没有留意书里的提示部分。大家可以自行选择看提示部分

在这里插入图片描述

# 借款本金
loan_amount = 10000

# 借款期限
number_of_year = 1

# 借款期限转换为月
number_of_month = number_of_year * 12

# 借款年利率
annual_interest_rate = 7

# 借款年利率转换为月利率
month_rate = annual_interest_rate / 1200 * loan_amount

# 每月支付额
month_payment = 865.26

# 总支付额
total_payment = 10383.21

# 输出
print(f"Loan Amount: {loan_amount}")
print(f"Number of Year: {number_of_year}")
print(f"Annual Interest Rate: {annual_interest_rate}")

print(f"""
Month payment(每月支付额):{month_payment}
Total payment:{total_payment}
""")
print("\t Payment(付款)#   Interest(利息)   Principal(本金)     Balance(结余)")

# 运用for循环来解答
for i in range(1, number_of_month + 1):
    loan_amount -= (month_payment - month_rate)

    print("  \t   ", i, "  \t     ", format(month_rate, ".2f"), "  \t        ", round(month_payment - month_rate, 2),
          "  \t      ", format(loan_amount, ".2f"))

    month_rate = annual_interest_rate / 1200 * loan_amount

输出内容如下

在这里插入图片描述

再修改了一些格式修饰部分

    print("\t\t", i, "     \t\t", format(month_rate, ".2f"), "   \t\t", round(month_payment - month_rate, 2),
          "   \t\t", format(loan_amount, ".2f"))

    month_rate = annual_interest_rate / 1200 * loan_amount

在这里插入图片描述

相对感觉格式好看点