我在这个Python数学习题集中遗漏了什么

我在这个Python数学习题集中遗漏了什么,python,math,calculator,Python,Math,Calculator,我在试着做一个计算器,告诉你在给定的工资、节省的百分比等条件下,为某栋房子的首期付款节省多少个月。我已经得到了代码,直到最后,我的数学在某个时候出错了,我不知道在哪里。任何帮助都会很好 说明: 年薪=浮动(输入('你的年薪是多少?\n')) 部分储蓄=浮动(输入('您每年将储蓄收入的百分之几(十进制)?\n')) 总成本=浮动(输入(“房屋的总成本是多少?\n”)) 部分首付=0.25*总成本 当前储蓄=0 月份=0 每月保存=(年薪/12)*部分保存 每月保存总数=(每月保存*.04)+每月保

我在试着做一个计算器,告诉你在给定的工资、节省的百分比等条件下,为某栋房子的首期付款节省多少个月。我已经得到了代码,直到最后,我的数学在某个时候出错了,我不知道在哪里。任何帮助都会很好

说明:

年薪=浮动(输入('你的年薪是多少?\n'))
部分储蓄=浮动(输入('您每年将储蓄收入的百分之几(十进制)?\n'))
总成本=浮动(输入(“房屋的总成本是多少?\n”))
部分首付=0.25*总成本
当前储蓄=0
月份=0
每月保存=(年薪/12)*部分保存
每月保存总数=(每月保存*.04)+每月保存
当活期存款<部分首付时:
活期存款=活期存款+月存款总额
月数=月数+1
其他:
打印(月)

我怀疑问题在于如何计算个人储蓄的利息。看起来你给了他们每一笔储蓄的额外4%,但这忽略了储蓄的复合性质。您可能希望将4%的计算转移到循环中,以便获得的利息基于所有储蓄余额:

current_savings = 0
months = 0
monthly_saved = (annual_salary / 12) * portion_saved

while current_savings < portion_down_payment:
    current_savings = current_savings * 1.04 + monthly_saved     # 4% monthly interest
    months = months + 1

print(months)
当前节省=0
月份=0
每月保存=(年薪/12)*部分保存
当活期存款<部分首付时:
活期存款=活期存款*1.04+月存款#4%月利息
月数=月数+1
打印(月)

现在,此代码假设您使用的4%是每月计算的适当利率。如果这是年利率(即APR),您可能需要将其除以12以获得正确的月利率。

在对其进行了一分钟的处理后,我意识到您需要在添加接下来的月工资之前计算您的利息,或者您接近但不完全…此代码输出与MITs测试用例相同

#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.

annual_salary = 0
total_cost = 0
portion_saved = 0


try:
    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: "))
except:
    print("Please make sure you input a number")

returns = 0.04
portion_down_payment = 0.25
down_payment = total_cost * portion_down_payment

monthly_salary = annual_salary / 12
monthly_save = monthly_salary * portion_saved
current_savings = 0

months = 1

while current_savings < down_payment:

    investments = (current_savings * returns) / 12 #investment return first otherwise you get close but not close enough
    current_savings += investments
    current_savings += monthly_save
    months += 1
#编写一个程序,计算你需要多少个月才能攒够钱
#买房子的钱
#付款。您希望您的主要变量是浮点数,所以您应该强制转换用户
#浮点数的输入。
年薪=0
总成本=0
保存的部分=0
尝试:
年薪=浮动(输入(“输入您的年薪:”)
部分保存=浮动(输入(“输入要保存的工资百分比,以小数形式:”)
总成本=浮动(输入(“输入您的梦想之家的成本:”)
除:
打印(“请确保您输入了一个数字”)
返回值=0.04
部分首付=0.25
首付=总成本*部分首付
月薪=年薪/12
月薪保存=月薪*部分保存
当前储蓄=0
月=1
当前存款<首付:
投资=(当前储蓄*回报)/12#首先是投资回报,否则你会接近但不够接近
当前储蓄+=投资
当前存款+=每月存款
月份+=1

安装此“月薪保存=(年薪/12)*部分保存”此“月薪保存=(年薪/12.0)*部分保存”有什么好处?欢迎使用Stack Overflow。你所说的“我的数学在某个时候出了问题”是什么意思?什么输入给了你错误的输出?请阅读并遵循。@user1438644:这只是Python 2中的一个问题。该用户似乎正在使用Python 3(其中,
print
是一个函数,
input
返回需要转换为数字的字符串,而不是直接转换为数字)。虽然由于年薪被显式转换为浮动,但无论如何它都应该在Python2中工作(
x/y
在Python2中仅当两个值都是整数时才截断)。
#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.

annual_salary = 0
total_cost = 0
portion_saved = 0


try:
    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: "))
except:
    print("Please make sure you input a number")

returns = 0.04
portion_down_payment = 0.25
down_payment = total_cost * portion_down_payment

monthly_salary = annual_salary / 12
monthly_save = monthly_salary * portion_saved
current_savings = 0

months = 1

while current_savings < down_payment:

    investments = (current_savings * returns) / 12 #investment return first otherwise you get close but not close enough
    current_savings += investments
    current_savings += monthly_save
    months += 1