它没有按预期工作。我该如何解决此代码?

时间:2019-11-03 07:23:57

标签: python arithmetic-expressions

我希望代码每六个月增加一次semi_annual_income,方法是使semi_annual income每六个月增加一个特定的百分比。因此,假设根据我的数学1000(1.2)^i/6,该等式每6个月0.2被6整除时,我的收入就会增加i

当我使用表达式1000(1.2)^i/6时,我试图同时使用这两个表达式,这会给我很大的数目。而表达式1000(1 +0.2)给了我1000(1 + 0.2)应该给我的确切答案。

number_of_months = 0
total_cost = 100000
semi_annual_income = 1000
starting_salary = 10
semi_annual_rise = 0.2
while semi_annual_income < total_cost:
    for i in range(100):
        if float(i)%6 == 0:
            power_number = float(i)/6
            # i am using this to make it increase the semi_annual income just only every six months
            semi_annual_income = semi_annual_income *(float(1) + float(semi_annual_rise))
            print(power_number)
            print(semi_annual_income)
            #semi_annual_income = (semi_annual_income *(float(1) +  float(semi_annual_rise))** float(power_number))
            #The above written code is giving me a very huge number i  want it to give me the answer as 1000(1 + 0.2)^i/6
    break

我从代码中得到了想要的答案,但我不明白为什么它会给我没有权力的答案,而有权力的人却没有给我答案。

1 个答案:

答案 0 :(得分:0)

number_of_months = 0
total_cost = 100000
semi_annual_income = 1000
starting_salary = 1000
semi_annual_rise = 0.2
number_of_months = 0
while semi_annual_income < total_cost:
    for i in range(1,100):
        if float(i)%6 == 0:
            power_number = float(i)/6# i am using this to make it increase the 
semi_annual income just only every six months
            number_of_months = number_of_months + 1
            semi_annual_income = starting_salary *(float(1) + 
float(semi_annual_rise))**(power_number)
            print(power_number)
            print(semi_annual_income)
            print(number_of_months)
# I think my mistake was the i used semi_annual-income instead of starting salary, this code works as i wanted it to be.
    break