【问题标题】:Stuck on a problem in MIT OCW 6.0001 problem set遇到 MIT OCW 6.0001 问题集中的一个问题
【发布时间】:2021-07-23 20:48:46
【问题描述】:

我绝对是 Python 的菜鸟(以及一般的编程),并且正在通过 MIT OCW 课程 (6.0001) 的帮助学习 Python。但是我目前遇到的问题集有一个问题:

你已经从麻省理工学院毕业,现在有一份很棒的工作!你移动到 旧金山湾区并决定开始储蓄购买 一个房子。由于湾区的房价非常高,你意识到 你将不得不储蓄几年才能负担得起 支付房子的首付。在 A 部分中,我们将 确定您需要多长时间才能节省足够的钱来完成 考虑到以下假设的首付:

  1. 调用您梦想家园的成本​total_cost​。
  2. 调用预付定金所需的部分费用​portion_down_payment​。为简单起见,假设 part_down_payment = 0.25 (25%)。
  3. 调用您迄今为止节省的金额​current_savings​。您从当前节省 0 美元开始。
  4. 假设您明智地投资当前的储蓄,年回报为​r​(换句话说,在每个月末,您收到 额外的​current_savings*r/12​资金用于您的储蓄 – 12 是因为​r​是年利率)。假设您的投资 获得 r = 0.04 (4%) 的回报。
  5. 假设您的年薪为​annual_salary​。
  6. 假设您打算每个月将一定数额的工资用于储蓄首付。调用该部分保存。 此变量应为十进制形式(即 0.1 表示 10%)。
  7. 在每个月末,您的储蓄将随着您的投资回报加上您月薪的一定百分比而增加 ​(年薪/12)。写一个程序计算多少个月 会带你存够首付的钱。你会 希望您的主要变量是浮点数,因此您应该转换用户输入 浮动。您的程序应该要求用户输入以下内容 变量:
  8. 起薪年薪(annual_salary)
  9. 要保存的工资部分 (portion_saved)
  10. 梦想家园的成本 (total_cost)

我为上面写的程序是:

annual_salary = float(input("Your starting annual salary: $"))
portion_saved = float(input("The portion of salary to be saved: "))
total_cost = float(input("The cost of your dream home: $"))
##Initial values
portion_down_payment = 0.25*total_cost
current_savings = 0
r = 0.04
num_months = 1
#(assuming that salary for a given month is received at the end of the month)
##The program
monthly_salary = annual_salary/12
investment_earnings = current_savings*r/12
while ((monthly_salary*portion_saved)+investment_earnings+current_savings) <= portion_down_payment:
    current_savings = current_savings + (monthly_salary*portion_saved)
    investment_earnings = current_savings*r/12
    current_savings = current_savings + investment_earnings
    num_months = num_months + 1
else:
    print("Number of months:",num_months)

我在上面的代码中面临的问题是,虽然我在其中一个测试用例上得到了正确答案,但在另一种情况下,我得到的月数比我应该得到的少一个根据问题集。

测试输入:

Enter your annual salary:​ 120000
Enter the percent of your salary to save, as a decimal:​ .10
Enter the cost of your dream home:​ 1000000

预期输出:

Number of months:​ 183

实际输出:

Number of months:​ 182

【问题讨论】:

  • 请张贴输入、实际输出和预期输出(输入程序时输入,从程序中获取时输出,两者肯定都不是文本!)
  • 嗨@Programmer,我已将输入和(实际和预期的)输出添加到问题中。
  • 谢谢,下次请你发布你的问题之前这样做!
  • 您能否为多个测试用例添加输入/预期+实际输出?有一个,我找不到错误……
  • 欢迎来到 Stack Overflow!请阅读How to debug small programs。您还可以使用Python-Tutor,它有助于逐步可视化代码的执行。

标签: python


【解决方案1】:

试试这个:

annual_salary = float(input('Enter your annual salary:​ ')) 
portion_saved = float(input('Enter the percent of your salary to save, as a decimal: ​'))
total_cost = float(input('Enter the cost of your dream home: ​')) 

# initial values
current_savings = 0  #the amount that you have saved so far,starting from 0
portion_down_payment = 0.25 * total_cost
current_savings = 0  #the amount that you have saved so far,starting from 0
r = 0.04 #annual rate
annual_return = (current_savings * r) / 12
monthly_salary = annual_salary / 12
portion_saved = portion_saved * monthly_salary 
num_months = 0

#Iterations and Output
while (current_savings <= portion_down_payment):
    num_months += 1
    current_savings += (current_savings * r / 12) + portion_saved    
print('Number of months: %d'%num_months)

【讨论】:

    猜你喜欢
    • 2022-07-16
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2017-09-10
    • 2012-06-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多