-1

我是python新手,所以这是我的问题。我在第 31 行的 y_list 上收到错误“IndentationError: expected an indented block” -> 在第一个 if 之后。我加载的文件包含很多数字,我们的想法是跳过所谓的 y_list 中的负数。

filename = "data_5.dat" # this file can also be found in the sandbox folder 
x_list = []
y_list = []

fp = open(filename)
for line in fp:
    var1, var2  = line.split(",")    # here we wish to split the line using the ',' character

    # since we want them in numeric format we need to convert
    a = float(var1)
    b = float(var2)

    # put them into two lists
    x_list.append(a)
    y_list.append(b)

fp.close()    # close the file


x = x_list
y = y_list

I = 0.0
L = 0.0

for k in range(1, len(x)):
if y_list>0:
        y_list.append(y)
        I += y[k-1] * (x[k] - x[k-1])

for k in range(1, len(x)):
if y_list>0:
        y_list.append(y)
        L += y[k] * (x[k] - x[k-1])

print I
print L
print (I+L)/2
4

3 回答 3

0
for k in range(1, len(x)):
if y_list>0:
        y_list.append(y)
        I += y[k-1] * (x[k] - x[k-1])

这些if块是块的一部分for,因此它们需要缩进一个:

for k in range(1, len(x)):
    if y_list>0:
        y_list.append(y)
        I += y[k-1] * (x[k] - x[k-1])

标准是使用四个空格,但是如果您混合制表符和空格,即使它在技术上是有效的 Python,也很容易遇到这样的问题。

于 2013-03-19T13:11:19.830 回答
0

转到第 31 行(然后是第 36 行)并缩进它。在行的开头点击 tab 键,这样它会在右边突出一点。

于 2013-03-19T13:06:45.440 回答
0
 #!/usr/bin/python

 import base64

 if naujas==20:
      Ivestis=input('Iveskite failo varda kuri noresite koreguoti')

 file2 = open(Ivestis, 'r')

 failas=file2.read() file2.close() file4=open(Ivestis,'r+')

 bfailas=failas.encode("UTF-8") efailas=base64.b64encode(bfailas)
 print(efailas)



 # encoded _data has the encoded form of our text – ’see the source’


 efailas=str(efailas)

 file4.write(efailas)

 file4.close()

 elif naujas==20:

 file3 = open('Naujas.txt', 'r+') 


 naujas=base64.b64decode(file3.read())

 print(naujas)


 else

 print('Tikriausiai tokio failo nera')

相同的

*不工作:/

于 2019-04-04T16:10:54.407 回答