MIT OCW 6.0001 课程 Problem Set 1 作业(Part A)

MIT OCW 6.0001 课程 Problem Set 1 作业(Part A)

打卡记录

打卡时间:2019.07.25

打卡天数:D02

学习内容:MIT OCW 6.0001 课程 Problem Set 1 作业(Part A)

参考链接

课程视频

作业链接

Part A 作业:

Part A: House Hunting

You have graduated from MIT and now have a great job! You move to the San Francisco Bay Area and decide that you want to start saving to buy a house. As housing prices are very high in the Bay Area, you realize you are going to have to save for several years before you can afford to make the down payment on a house. In Part A, we are going to determine how long it will take you to save enough money to make the down payment given the following assumptions:

  1. Call the cost of your dream home total_cost.

  2. Call the portion of the cost needed for a down payment portion_down_payment. For simplicity, assume that portion_down_payment = 0.25 (25%).

  3. Call the amount that you have saved thus far current_savings. You start with a current savings of $0.

  4. Assume that you invest your current savings wisely, with an annual return of r (in other words, at the end of each month, you receive an additional current_savings*r/12 funds to put into your savings – the 12 is because r is an annual rate). Assume that your investments earn a return of r = 0.04 (4%).

  5. Assume your annual salary is annual_salary.

  6. Assume you are going to dedicate a certain amount of your salary each month to saving for the down payment. Call that portion_saved. This variable should be in decimal form (i.e. 0.1 for 10%).

  7. At the end of each month, your savings will be increased by the return on your investment, plus a percentage of your monthly salary (annual salary / 12).

Write a program to calculate how many months it will take you to save up enough money for a down payment. You will want your main variables to be floats, so you should cast user inputs to floats.

Your program should ask the user to enter the following variables:

  1. The starting annual salary (annual_salary)
  2. The portion of salary to be saved (portion_saved)
  3. The cost of your dream home (total_cost)

作业心得

光看懂英文作业已经蛮吃力了,哈哈,简化作业说明如下:

  1. 买房子的总费用为 total_cost
  2. 首付(down payment)为25%, portion_down_payment = 0.25
  3. 存款为 current_savings,从0开始
  4. 存款有投资,年回报率(an annual return)为r(0.04),所以每月额外收入为 current_savings*r/12
  5. 年薪为 annual_salary
  6. 从每月薪资中,固定比例用于首付,这个比率设为 portion_saved
  7. 每个月的收入来源,主要是 投资回报 + 每月工资 monthly salary(annual salary / 12)

写一个程序用于计算 需要多少个月储蓄才够首付,大多数变量都需要float类型,所以需要将用户的输入转化为float,程序需要用户输入以下变量::

  1. 一开始的年薪 (annual_salary)
  2. 每月存款的比例 (portion_saved)
  3. 房子的价钱 (total_cost)

整体的思路就是做一个循环,每个迭代就是一个月,判断当月 的存款是否大于等于首付。

存款由三部分组成:

  1. 已有存款
  2. 月薪按比例存款
  3. 每月投资回报

程序代码

# 起始年薪 
# annual_salary = 120000 
annual_salary = float(input("Enter your annual salary:")) 

# 每月薪资存款比例 
# portion_saved = .10 
portion_saved = float(input("Enter the percent of your salary to save, as a decimal:")) 

# 房价 
# total_cost = 1000000 

total_cost = float(input("Enter the cost of your dream home:")) 

# 首付比例 
portion_down_payment = 0.25 

# 存款年化率 
r = 0.04 

# 当前存款 
current_savings = 0 
month_count = 1 
# 首付 
down_payment = total_cost * portion_down_payment 

while True: 
    # 计算当月存款 = 存款 + 月薪*每月薪资存款比例 + 投资回报 
    current_savings = current_savings + annual_salary/12*portion_saved +     current_savings*r/12 
    if current_savings >= down_payment: 
        print("Number of months: {}".format(month_count)) 
        break 
    # 月份自增 
    month_count += 1 
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容