python存款买房

任务描述

本关任务:编写一个计算存款买房的小程序。

相关知识

存款买房问题描述

你刚刚大学毕业,在北京找到了一份很好的工作,决定开始存钱买房。由于北京的房价很高,你要攒几年钱才能付得起房子的首付。

现根据以下假定来计算你需要多长时间才能攒够首付款:

  1. 将你想购买的房子的总价称为 total_cost

  1. 将首付款占总房价的比例称为 portion_down_payment。为简单起见,假设 portion_down_payment = 0.30(30%)

  1. 将存款金额称为 current_savings。你的存款从 0 元开始。

  1. 假设你的年薪是 annual_salary,按 12 个月平均发放,单位是元。

  1. 假设你每个月都要拿出一定百分比的工资来存首付。称为 portion_saved,此值为一个表示百分比的整数,例如 50 表示 50%。

写一个程序来计算你需要多少个月才能攒够钱付定金,不足一个月按一个月计算。

第1关 存款买房-A

import math
total_cost = float(input())           # '请输入总房价:'total_cost为当前房价
annual_salary = float(input())        # '请输入年薪:'
portion_saved = float(input()) / 100  # '请输入月存款比例:'月存款比例,输入30转为30%

# 根据首付款比例计算首付款金额和每个月需要存款数额
# 补充你的代码
down_payment = total_cost *0.3
monthly_deposit = annual_salary * portion_saved /12

print(f'首付 {down_payment:.2f} 元', )
print(f'月存款 {monthly_deposit:.2f} 元')

# 计算多少个月才能存够首付款,结果为整数,不足1月按1个月计算,即向上取整
# 补充你的代码
number_of_months=down_payment / monthly_deposit
print(f'需要{math.ceil(number_of_months)}个月可以存够首付')

第2关 存款买房-B

total_cost = float(input())           # total_cost为当前房价
annual_salary = float(input())        # 年薪
portion_saved = float(input()) / 100  # 月存款比例,输入30转为30%
semi_annual_raise = float(input()) /100     # 输入每半年加薪比例,输入7转化为7%

portion_down_payment = 0.3      # 首付比例,浮点数
# 补充你的代码,计算首付款     
down_payment = total_cost * 0.3

print(f'首付 {down_payment:.2f} 元')

current_savings = 0                                # 存款金额,从0开始
number_of_months = 0
monthly_salary = annual_salary/12                  # 月工资
monthly_deposit = monthly_salary * portion_saved   # 月存款
# 计算多少个月才能存够首付款,结果为整数,不足1月按1个月计算,即向上取整
# 补充你的代码 
while True:
    current_savings = current_savings + monthly_deposit
    number_of_months = number_of_months + 1
    if current_savings >= down_payment:  # 存够了买房去
        break
    if number_of_months % 6 == 0:
        monthly_deposit = monthly_deposit * (1 + semi_annual_raise)
    if number_of_months % 12 == 0:
        print("第{}个月月末有{:,.0f}元存款".format(number_of_months, current_savings))
   
print(f'需要{number_of_months}个月可以存够首付')

第3关 存款买房-C

total_cost = float(input())  # total_cost为当前房价
annual_salary = float(input())  # 年薪
portion_saved = float(input()) / 100  # 月存款比例,输入30转为30%
semi_annual_raise = float(input()) / 100  # 输入每半年加薪比例,输入7转化为7%

portion_down_payment = 0.3  # 首付比例,浮点数
down_payment = portion_down_payment * total_cost  # 首付款
print(f'首付 {down_payment:.2f} 元')

current_savings = 0  # 存款金额,从0开始
number_of_months = 0
# 补充你的代码,计算月工资,计算月存款
monthly_deposit = annual_salary / 12 *portion_saved
a = monthly_deposit
while True:
    number_of_months = number_of_months + 1
    current_savings += 2.25 * 0.01 * current_savings / 12
    current_savings = current_savings + monthly_deposit
    if number_of_months % 6 == 0:
        monthly_deposit = monthly_deposit * (1 + semi_annual_raise)
    if number_of_months % 12 == 0:
        print("第{}个月月末有{:,.0f}元存款".format(number_of_months, current_savings))
    if current_savings >= down_payment:
        break
print(f'需要{number_of_months}个月可以存够首付')

# 补充你的代码,计算多少个月才能存够首付款,结果为整数,不足1月按1个月计算,即向上取整

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年4月6日
下一篇 2023年4月6日

相关推荐