Python:IndentationError:期望一个缩进块

时间:2015-12-30 14:50:19

标签: python

#Returns the sum of num1 and num2
while True:
  def add(num1, num2):
    return num1 + num2
  def sub(num1, num2):
    return num1 - num2

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

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

#*, -, /, +
#remember brackets work like normal
  def main ():
    operation = input("What type of calculation would you like to make? +,-,/,*")
    if(operation != '+' and operation != '-' and operation != '/' and operation != '*'):
    #operation invalid
      print("To continue enter a vaild operation")
    else:
      var1 = int(input("Enter num1:"))
      var2 = int(input("Enter num1:"))
      if(operation == '+'):
        print(add(var1, var2))
      elif(operation == '/'):
        print(div(var1, var2))
      elif(operation == '*'):
        print(mul(var1, var2))
      elif(operation == '-'):
        print(sub(var1, var2))
       #Restarts the calculator
    while True:
        answer = raw_input('Run again? (y/n): ')
        if answer in ('y', 'n'):
            break
        print ("Invalid input.")
    if answer == "y":
 continue
    else:
        print ('Goodbye')
        break
main(add)

嗨,初学者在这里。我得到了IndentationError: unindent does not match any outer indentation level。你能帮忙的话,我会很高兴。如果我再继续下去,我会得到IndentationError: expected an indented block。不知道该怎么做。

3 个答案:

答案 0 :(得分:1)

我明白了

File "", line 24
    continue
           ^
IndentationError: expected an indented block

您的功能应如下所示

def main ():
    operation = input("What type of calculation would you like to make? +,-,/,*")
    if(operation != '+' and operation != '-' and operation != '/' and operation != '*'):
    #operation invalid
      print("To continue enter a vaild operation")
    else:
      var1 = int(input("Enter num1:"))
      var2 = int(input("Enter num1:"))
      if(operation == '+'):
        print(add(var1, var2))
      elif(operation == '/'):
        print(div(var1, var2))
      elif(operation == '*'):
        print(mul(var1, var2))
      elif(operation == '-'):
        print(sub(var1, var2))
       #Restarts the calculator
    while True:
        answer = raw_input('Run again? (y/n): ')
        if answer in ('y', 'n'):
            break
        print ("Invalid input.")
        if answer == "y":
            continue
        else:
            print ('Goodbye')
            break

答案 1 :(得分:0)

我纠正了许多错误的代码块。还纠正了其他错误。尽量避免while True:循环,因为您的程序很容易陷入其中并导致意外后果。

# Returns the sum of num1 and num2

def add(num1, num2):
    return num1 + num2


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


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


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


# *, -, /, +
# remember brackets work like normal
def main():
    operation = raw_input("What type of calculation would you like to make? +,-,/,*")
    if (operation != '+' and operation != '-' and operation != '/' and operation != '*'):
        # operation invalid
        print("To continue enter a vaild operation")
    else:
        var1 = int(raw_input("Enter num1:"))
        var2 = int(raw_input("Enter num1:"))
        if (operation == '+'):
            print(add(var1, var2))
        elif (operation == '/'):
            print(div(var1, var2))
        elif (operation == '*'):
            print(mul(var1, var2))
        elif (operation == '-'):
            print(sub(var1, var2))
    # Restarts the calculator

main()
answer = raw_input('Run again? (y/n): ')
while answer == "y":
    main()
    answer = raw_input('Run again? (y/n): ')

答案 2 :(得分:0)

由于缩进不良而发生错误。在Python中,错误的缩进意味着奇数个空格。例如,0,1或3个空格是不良缩进。

例如

def main():
    pass

是有效缩进,因为pass函数下的语句main有4个空格;相反,如果语句passmain下有0,1或3个空格。有效缩进包括偶数个空格2,4等。

嗯,简而言之,Python中的缩进应该是2或4个空格。