【问题标题】:It is not working as expected. How can i fix this code?它没有按预期工作。我该如何修复此代码?
【发布时间】:2019-11-03 07:23:57
【问题描述】:

我希望代码每六个月增加一次我的semi_annual_income,方法是使semi_annual income 每六个月增加一个特定的百分比。所以假设根据我的数学1000(1.2)^i/6,这个等式将每 6 个月增加我的收入0.2,其中i 可以被 6 整除。

当我使用表达式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

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

【问题讨论】:

    标签: python arithmetic-expressions


    【解决方案1】:
    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
    

    【讨论】:

      猜你喜欢
      • 2015-12-12
      • 2022-10-13
      • 1970-01-01
      • 2022-10-06
      • 2023-02-21
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      相关资源
      最近更新 更多