Python 麻省理工学院开放式课程6.0001习题集中的一个问题
我对python(以及一般编程)是个十足的新手,我通过麻省理工学院开放式课程(6.0001)学习python。但其中一个习题集上有一个问题,我目前一直在做: 你已经从麻省理工学院毕业了,现在有一份很棒的工作!你搬去医院 旧金山湾地区,并决定你想开始储蓄购买 房子。由于湾区的房价很高,你会意识到 在你能负担得起之前,你必须储蓄好几年 支付房子的首期款。在A部分中,我们将 确定你需要多长时间才能存足够的钱来赚钱 根据以下假设支付首付款:Python 麻省理工学院开放式课程6.0001习题集中的一个问题,python,Python,我对python(以及一般编程)是个十足的新手,我通过麻省理工学院开放式课程(6.0001)学习python。但其中一个习题集上有一个问题,我目前一直在做: 你已经从麻省理工学院毕业了,现在有一份很棒的工作!你搬去医院 旧金山湾地区,并决定你想开始储蓄购买 房子。由于湾区的房价很高,你会意识到 在你能负担得起之前,你必须储蓄好几年 支付房子的首期款。在A部分中,我们将 确定你需要多长时间才能存足够的钱来赚钱 根据以下假设支付首付款: 给你梦想的家打电话总成本. 要求支付首期付款所需的部分费用
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)
预期产出:
Number of months: 183
实际产量:
Number of months: 182
试试这个:
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)
年薪=浮动(输入('输入您的年薪: '))
部分保存=浮动(输入('输入要保存的工资百分比,以小数形式:'))
总成本=浮动(输入('输入您的梦想之家的成本:'))
#初始值
当前储蓄=0#到目前为止您已储蓄的金额,从0开始
部分首付=0.25*总成本
当前储蓄=0#到目前为止您已储蓄的金额,从0开始
r=0.04#年利率
年收益率=(当前储蓄*r)/12
月薪=年薪/12
保存部分=保存部分*月薪
月数=0
#迭代和输出
虽然(当前节省的费用请公布输入、实际输出和预期输出(输入时输入到程序中,输出时从程序中获取,两者肯定不是文本!)嗨@Programmer,我已经添加了输入和(实际和预期)输出到问题。谢谢,下次发布问题之前请先这样做!您能为多个测试用例添加输入/输出+实际输出吗?有一个,我找不到错误…欢迎使用堆栈溢出!请阅读。您还可以使用它帮助逐步可视化代码的执行。
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)