2017-04-25 91 views
-2
File "first.py", line 37 
    elif(operation == "quadratic formula") 
IndentationError: unindent does not match any outer indentation level 

我真的不知道我的代碼出現了什麼問題。有人可以幫忙嗎?IndentationError:unindent不匹配任何外部縮進級別

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)) 
+5

停止混合標籤和空格。另外,C與這些沒有任何關係。 – user2357112

回答

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)) 
+0

根本沒有工作,實際上犯了螞蟻錯誤:( –

+0

謝謝你的工作<3 –

+0

伊甸園:不要提及它,真的(見[_我應該怎麼做當有人回答我的問題?_](http:// stackoverflow .com/help/someone-answers)) - 並且遠離那些受到製表符字符';-)'。 – martineau

相關問題