为什么我会在缩进中“不一致地使用制表符和空格”?

时间:2015-10-27 15:08:48

标签: python tabs indentation

这是我的代码:

x = [];
y = [];

xx = []
xy = []


# variable to show if file openening worked

opened = 0;

# try to open file

try:
    readFile = open('xyData.txt', 'r');
    # if open file worked
    opened = 1;

except:
    # if opening file went wrong
    print('Some error occured!');

#next line same as if opened == 1
if opened:
    # read in data from the file line by line

    for line in readFile:
        # variable line now holds one of the lines
        # in the data file
        # split up in the string 'line' based on whitespace
        splitUp = line.split();





        # x = splitUp[0], y = splitUp[1]
        # append to arrays for x and y
        x.append(splitUp[0]);
        y.append(splitUp[1]);
        xx.append(splitUp[0]*splitUp[0]);
        xy.append(splitUp[0]*splitUp[1]);



    # close file
    readFile.close();

  print('done')

此时我还有更多工作要做:

run lobf.py
  File "/Users/paulbebb/Desktop/pyscripts/lobf.py", line 56
    x.append(splitUp[0]);
                     ^
TabError: inconsistent use of tabs and spaces in indentation

我似乎无法解决问题。提前谢谢。

4 个答案:

答案 0 :(得分:0)

看起来你有一些奇怪的标签和空格组合。坚持只使用标签或空格。以下代码仅使用空格。尝试将下面的代码粘贴到您的编辑器中。

x = []
y = []

xx = []
xy = []


# variable to show if file openening worked

opened = 0

# try to open file

try:
    readFile = open('xyData.txt', 'r')
    # if open file worked
    opened = 1

except:
    # if opening file went wrong
    print('Some error occured!')

#next line same as if opened == 1
if opened:
    # read in data from the file line by line

    for line in readFile:
        # variable line now holds one of the lines
        # in the data file
        # split up in the string 'line' based on whitespace
        splitUp = line.split()





        # x = splitUp[0], y = splitUp[1]
        # append to arrays for x and y
        x.append(splitUp[0])
        y.append(splitUp[1])
        xx.append(splitUp[0]*splitUp[0])
        xy.append(splitUp[0]*splitUp[1])



    # close file
    readFile.close()

    print('done')

答案 1 :(得分:0)

你的打印上的缩进似乎是错误的...试试这个...你也不需要在每个语句之后;,因为这是python

x = []
y = []

xx = []
xy = []

# variable to show if file openening worked

opened = 0;

# try to open file

try:
    readFile = open('xyData.txt', 'r')
    # if open file worked
    opened = 1

except:
    # if opening file went wrong
    print('Some error occured!')

#next line same as if opened == 1
if opened:
    # read in data from the file line by line
    for line in readFile:
        # variable line now holds one of the lines
        # in the data file
        # split up in the string 'line' based on whitespace
        splitUp = line.split()

        # x = splitUp[0], y = splitUp[1]
        # append to arrays for x and y
        x.append(splitUp[0])
        y.append(splitUp[1])
        xx.append(splitUp[0]*splitUp[0])
        xy.append(splitUp[0]*splitUp[1])

    # close file
    readFile.close();

    print('done')

答案 2 :(得分:0)

一切都必须在空间中。每个标签是4个空格。我会确保你正在标记4个空格而不是标签。基本上,请确保您没有任何标签,并且只有缩进空格。

答案 3 :(得分:0)

您的编辑器应该可以选择如何处理Tab键。我知道在notepad ++中你可以在选择特定语言时将tab设置为x个空格。