Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 麻省理工学院开放式课程6.0001习题集中的一个问题_Python - Fatal编程技术网

Python 麻省理工学院开放式课程6.0001习题集中的一个问题

Python 麻省理工学院开放式课程6.0001习题集中的一个问题,python,Python,我对python(以及一般编程)是个十足的新手,我通过麻省理工学院开放式课程(6.0001)学习python。但其中一个习题集上有一个问题,我目前一直在做: 你已经从麻省理工学院毕业了,现在有一份很棒的工作!你搬去医院 旧金山湾地区,并决定你想开始储蓄购买 房子。由于湾区的房价很高,你会意识到 在你能负担得起之前,你必须储蓄好几年 支付房子的首期款。在A部分中,我们将 确定你需要多长时间才能存足够的钱来赚钱 根据以下假设支付首付款: 给你梦想的家打电话​总成本​. 要求支付首期付款所需的部分费用

我对python(以及一般编程)是个十足的新手,我通过麻省理工学院开放式课程(6.0001)学习python。但其中一个习题集上有一个问题,我目前一直在做:

你已经从麻省理工学院毕业了,现在有一份很棒的工作!你搬去医院 旧金山湾地区,并决定你想开始储蓄购买 房子。由于湾区的房价很高,你会意识到 在你能负担得起之前,你必须储蓄好几年 支付房子的首期款。在A部分中,我们将 确定你需要多长时间才能存足够的钱来赚钱 根据以下假设支付首付款:

  • 给你梦想的家打电话​总成本​.
  • 要求支付首期付款所需的部分费用​部分首付​. 为了简单起见,假设 部分首付=0.25(25%)
  • 调用您迄今为止已保存的金额​活期存款​. 你从目前的0美元储蓄开始
  • 假设您明智地投资您的当前储蓄,年回报率为​R​(换句话说,在每个月底,您收到 额外的​活期存款*r/12​ 用于储蓄的资金- 12是因为​R​ 是年利率)。假设你的投资 收益率r=0.04(4%)
  • 假设你的年薪是​年薪​.
  • 假设你每个月都要拿出一定数额的薪水来储蓄首期付款。叫它​保存的部分​. 该变量应为十进制形式(即0.1代表10%)
  • 在每个月底,你的储蓄会随着你的投资回报率,再加上你收入的一个百分比而增加​月薪 ​(年薪/12)。写一个程序来计算它有多少个月 这会让你存足够的钱来付定金。你会 希望您的主要变量是浮点数,所以您应该强制转换用户输入 漂浮。您的程序应该要求用户输入以下内容 变量:
  • 起始年薪(年薪)
  • 要保存的工资部分(保存的部分)
  • 你梦想之家的成本(总成本)
  • 我为上述内容编写的程序是:

    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)