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

时间:2014-12-23 17:45:23

标签: python

我正在阅读的书建议我使用函数,whiles和if来创建程序。

该计划的目的是:

  

从用户那里取4个数字,2个将被加总,2个将被减去   有3个功能。一个是procedure_1,它是一段时间,它应该从用户获取数字并继续该过程,直到sum达到< = 100并且sub达到< = 100的值,它还将这些过程的结果放在一个名单。如果是这样,start()函数有一个if,如果它达到这些值,它将运行procedure_2,它打印一条消息并打印列表的结果。

继承代码,但我得到了:

print "Results of sum and subtract:... %d, %d" % (sum, rest)
                                                               ^
IndentationError: unindent does not match any outer indentation

    sum = 0
rest = 0
results = []
results_2 = []

def procedure_1():
    while sum <= 100 and rest <= 100:
        print "What numbers do you wish to sum and subtract?"
        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")

        inner_sum = n1 + n2
        inner_rest = ns1 - ns2

        print "Results of sum and subtract:... %d, %d" % (inner_sum, inner_rest)
        results.append(inner_sum); results_2.append(inner_rest)

        sum += inner_sum
        rest += inner_rest

def procedure_2():
    print "Values are too high to compute your stuff"
    for inner_sum in results:
    print sum
    for inner_rest in results_2:
        print rest


def start():
    if sum < 100 and rest < 100:
        procedure_1()
    else:
        procedure_2()

start()

检查并仔细检查它,仍然无法运行它并看到它有什么问题,非常感谢有关如何使此代码工作的建议。非常感谢。

编辑。 我得到了这个新错误:在赋值之前引用了局部变量'sum'。尝试将全局的一个放在开头,也删除它们,但没有运气。 Book还没有向我提起过这个。

1 个答案:

答案 0 :(得分:3)

您需要使这些行的缩进与while循环的其余部分匹配:

        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")

所以它看起来像这样:

def procedure_1():
    while sum <= 100 and rest <= 100:
        sum = n1 + n2; rest = ns1 - ns2
        print "What numbers do you wish to sum and subtract?"
        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")
        print "Results of sum and subtract:... %d, %d" % (sum, rest)
        results.append(sum); results_2.append(rest)

        sum += sum; rest += rest

但是,在您要求输入后,您可能还想移动总和的计算等等:

def procedure_1():
    while sum <= 100 and rest <= 100:
        print "What numbers do you wish to sum and subtract?"
        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")

        # Note: It's generally considered bad form to use semicolons in Python.
        sum = n1 + n2
        rest = ns1 - ns2

        print "Results of sum and subtract:... %d, %d" % (sum, rest)
        results.append(sum); results_2.append(rest)

        sum += sum
        rest += rest

最后,您不希望两个不同的变量具有相同的名称,因此您的内部变量需要具有不同的名称,以免影响全局变量:

def procedure_1():
    while sum <= 100 and rest <= 100:
        print "What numbers do you wish to sum and subtract?"
        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")

        inner_sum = n1 + n2
        inner_rest = ns1 - ns2

        print "Results of sum and subtract:... %d, %d" % (inner_sum, inner_rest)
        results.append(inner_sum); results_2.append(inner_rest)

        sum += inner_sum
        rest += inner_rest

最后,因为sumrest是您想要从函数内部修改的全局变量,您需要注意您要写入全局版本:

def procedure_1():
    # This says to write to the global variables rather than creating local ones.
    global sum, rest

    while sum <= 100 and rest <= 100:
        print "What numbers do you wish to sum and subtract?"
        n1 = raw_input("Sum Num1:...") 
        n2 = raw_input ("Sum num2:...")
        ns1 = raw_input("Sub Num 1:...")
        ns2 = raw_input ("Sub Num 2:...")

        inner_sum = n1 + n2
        inner_rest = ns1 - ns2

        print "Results of sum and subtract:... %d, %d" % (inner_sum, inner_rest)
        results.append(inner_sum); results_2.append(inner_rest)

        sum += inner_sum
        rest += inner_rest

(另请注意sum是Python中的一个函数,因此您可能希望将变量命名为其他内容。)