我有一个缩进错误,我不知道如何解决

时间:2020-04-17 12:50:53

标签: python

我目前正在做an assignment for the MIT introduction to python,C部分。我不断得到:

IndentationError: unindent does not match any outer indentation level  

代码如下:

#MIT C
# setting up varaibles
interest = float(0.04)
semi_annual_raise = float(0.07)
total_cost=1000000
portion_down_payment=0.25
downpayment=total_cost*portion_down_payment
starting_annual_salary=float(input("Enter the starting salary: "))
monthly_salary=starting_annual_salary/12
current_savings = 0
# setting up guess
programe = 1 
low=0
high = 1
step = 0
month = 1
guess = (high+low)/2.0
tolerance=500
savings = 0
# semi annual raise code
while programe == 1:
        end_salary = (monthly_salary * 36) * (interest * 3)
        current_savings = guess * end_salary
        savings = guess * end_salary
        if current_savings >  downpayment +- tolerance:
             print("the savings rate was too high ," , guess)
             print("reunning program")
             low = guess
             guess = (high + low) / 2.0
             current_savings = guess * end_salary
             savings = guess * end_salary
        elif current_savings < downpayment +- tolerance:
             print("the savings rate was too low ," , guess)
             print("rerunning program")
             high = guess
             guess = (high+low)/2.0
             current_savings = guess * end_salary
             savings = guess * end_salary
         else savings == downpayment +- tolerance:
             print("enter starting salary: " , starting_annual_salary)
             print("best savings rate:" , guess)
             print("steps in bisection search" , step)
             programe = 0.5
        continue
    if programe == 0.5:
        break all

2 个答案:

答案 0 :(得分:1)

我对代码进行了一些修改,使其可以正常运行而不会出现错误消息:

#MIT C
# setting up varaibles
interest = float(0.04)
semi_annual_raise = float(0.07)
total_cost=1000000
portion_down_payment=0.25
downpayment=total_cost*portion_down_payment
starting_annual_salary=float(input("Enter the starting salary: "))
monthly_salary=starting_annual_salary/12
current_savings = 0
# setting up guess
programe = 1 
low=0
high = 1
step = 0
month = 1
guess = (high+low)/2.0
tolerance=500
savings = 0
# semi annual raise code
while programe == 1:
    end_salary = (monthly_salary * 36) * (interest * 3)
    current_savings = guess * end_salary
    savings = guess * end_salary
    if current_savings >  downpayment +- tolerance:
        print("the savings rate was too high ," , guess)
        print("reunning program")
        low = guess
        guess = (high + low) / 2.0
        current_savings = guess * end_salary
        savings = guess * end_salary
    elif current_savings < downpayment +- tolerance:
        print("the savings rate was too low ," , guess)
        print("rerunning program")
        high = guess
        guess = (high+low)/2.0
        current_savings = guess * end_salary
        savings = guess * end_salary
    elif savings == downpayment +- tolerance:
        print("enter starting salary: " , starting_annual_salary)
        print("best savings rate:" , guess)
        print("steps in bisection search" , step)
        programe = 0.5
    continue
    if programe == 0.5:
        break

但是,我认为它陷入了持续的循环。我不确定您希望程序执行什么操作,因此无法修复。

答案 1 :(得分:0)

end_salarycontinue语句的所有内容看起来都缩进了8个空格,而不是4个空格。

看起来每个if语句下的所有行都缩进5个空格而不是4个空格。

尝试将其拉回,您应该看到错误消失了。

这是我清理过的代码:

#MIT C
# setting up varaibles
interest = float(0.04)
semi_annual_raise = float(0.07)
total_cost=1000000
portion_down_payment=0.25
downpayment=total_cost*portion_down_payment
starting_annual_salary=float(input("Enter the starting salary: "))
monthly_salary=starting_annual_salary/12
current_savings = 0
# setting up guess
programe = 1 
low=0
high = 1
step = 0
month = 1
guess = (high+low)/2.0
tolerance=500
savings = 0
# semi annual raise code
while programe == 1:
    end_salary = (monthly_salary * 36) * (interest * 3)
    current_savings = guess * end_salary
    savings = guess * end_salary
    if current_savings >  downpayment +- tolerance:
        print("the savings rate was too high ," , guess)
        print("reunning program")
        low = guess
        guess = (high + low) / 2.0
        current_savings = guess * end_salary
        savings = guess * end_salary
    elif current_savings < downpayment +- tolerance:
        print("the savings rate was too low ," , guess)
        print("rerunning program")
        high = guess
        guess = (high+low)/2.0
        current_savings = guess * end_salary
        savings = guess * end_salary
    else savings == downpayment +- tolerance:
        print("enter starting salary: " , starting_annual_salary)
        print("best savings rate:" , guess)
        print("steps in bisection search" , step)
        programe = 0.5
    continue
    if programe == 0.5:
        break all