IndentationError:unindent与任何外部缩进级别都不匹配

时间:2017-04-25 21:23:47

标签: python python-3.x

File "first.py", line 37
  elif(operation == "quadratic formula")
IndentationError: unindent does not match any outer indentation level

我真的不知道我的代码有什么问题。有人可以帮忙吗?

def add(var1, var2):
  return var1 + var2

def sub(num1, num2):
  return num1 - num2

def div(num1, num2):
    return num1/num2

def mul(num1,num2):
    return num1*num2
def equation1():
 import math

 a = int(raw_input("please enter your a in the equation: "))
 b = int(raw_input("please enter your b in the equation: "))
 c = int(raw_input("please enter your c in the equation: "))

 d = b**2-4*a*c 

 if(d < 0):
    print "This equation has no real solution"
 elif(d == 0):
    x = (-b+math.sqrt(b**2-4*a*c))/(2*a)
    print "This equation has one solutions: ", x
 else:
    x1 = (-b+math.sqrt(b**2-4*a*c))/(2*a)
    x2 = (-b-math.sqrt(b**2-4*a*c))/(2*a)
    print "This equation has two solutions"
    print "The first soluition: ", x1
    print "And the second solution", x2

def main():
    operation = raw_input("what do you want to do (+,-,/,*) or quadratic 
    formula? ")
    if(operation != '+' and operation != '-' and operation != '/' and operation != '*' and operation != 'quadratic formula'):
        print 'You must put a valid operation'
    elif( operation == "quadratic formula" ):
        equation1()
    else:
        var1 = int(raw_input("enter the first number:  " ))
        var2 = int(raw_input("enter the second number: "))
        if(operation == '+'):
            print (add(var1, var2))
        elif(operation == '/'):
            print (div(var1, var2))
        elif(operation == '-'):
            print (sub(var1, var2))

1 个答案:

答案 0 :(得分:1)

以下显示了我的文本编辑器中的代码,其中选项卡和空格字符可见并以红色显示。正如您所看到的,它表明您正在混合使用它们 - 这使Python解释器感到困惑。最好坚持使用其中一种,尽管我认为4个空间是最好的(也是最便携的)。

您可以将编辑器配置为自动将标签转换为正确数量的空格。

screenshot of code in editor

<强>更新

由于您似乎仍然遇到问题,这里是您的代码版本,我已删除了标签字符,所以现在它的缩进仅包含每个级别的4个空格字符。看看你是否有更好的运气。

def add(var1, var2):
    return var1 + var2

def sub(num1, num2):
    return num1 - num2

def div(num1, num2):
    return num1/num2

def mul(num1,num2):
    return num1*num2

def equation1():
    import math

    a = int(raw_input("please enter your a in the equation: "))
    b = int(raw_input("please enter your b in the equation: "))
    c = int(raw_input("please enter your c in the equation: "))

    d = b**2-4*a*c

    if(d < 0):
        print "This equation has no real solution"
    elif(d == 0):
        x = (-b+math.sqrt(b**2-4*a*c))/(2*a)
        print "This equation has one solutions: ", x
    else:
        x1 = (-b+math.sqrt(b**2-4*a*c))/(2*a)
        x2 = (-b-math.sqrt(b**2-4*a*c))/(2*a)
        print "This equation has two solutions"
        print "The first soluition: ", x1
        print "And the second solution", x2

def main():
    operation = raw_input("what do you want to do (+,-,/,*) or quadratic formula? ")
    if(operation != '+' and operation != '-' and operation != '/' and operation != '*' and operation != 'quadratic formula'):
        print 'You must put a valid operation'
    elif( operation == "quadratic formula" ):
        equation1()
    else:
        var1 = int(raw_input("enter the first number:  " ))
        var2 = int(raw_input("enter the second number: "))
        if(operation == '+'):
            print (add(var1, var2))
        elif(operation == '/'):
            print (div(var1, var2))
        elif(operation == '-'):
            print (sub(var1, var2))