gpt4 book ai didi

python - 缩进错误: unindent does not match any outer indentation

转载 作者:行者123 更新时间:2023-11-30 23:16:41 25 4
gpt4 key购买 nike

我正在阅读的书建议我使用函数、while 和 if 编写一个程序。

该计划的目的是:

Take 4 numbers from the user, 2 will be summed, 2 will be subtracted.
There are 3 functions. One is procedure_1, it is a while, it is supposed to take numbers from the user and continue the process until sum reaches <= 100 and sub reaches a value of <= 100, it also puts the results of these procedures on a list. In case it does, start() function has an if that, in case it reaches those values, it will run procedure_2, which prints a message and also prints the results of the lists.

这是代码,但我得到:

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也没有向我提到过这一点。

最佳答案

您需要使这些行的缩进与 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 中的函数,因此您可能希望将变量命名为其他名称。)

关于python - 缩进错误: unindent does not match any outer indentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27625458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com