0

I'm trying to use this simple function:

def add(*numbers):
    print(numbers)
    return sum(numbers)

x = add(1, 2, 3)
print(x)

And I'm getting this error:

File "exp.py", line 3
    return sum(numbers)
                      ^
TabError: inconsistent use of tabs and spaces in indentation

The instructor at my online course is doing absolutely the same thing and is not getting any errors, when running the file in command line. Anyone knows why is that?

  • I just typed the exact same thing into a python script, and the behavior was exactly as expected. Are you sure you're using *only* tabs or *only* spaces in the two lines that constitute the function? It seems like the interpreter is complaining that the two are intermixed. – Matthew P. May 14 '20 at 00:31
  • Thank you. I tried to play around with deleting the empty spaces and placing the parts of the function anew and now it's working. But I don't understand what exactly was wrong before, it looks the very same.. – Nikolay Tonev May 14 '20 at 00:46
  • a (single) tab character is different than (four) space characters – Namaskar May 14 '20 at 01:41
  • @NikolayTonev Sven is correct, the interpreter is very particular about indentation consistency. Visually, we see that spaces and tabs give the exact same result, but in the hex code that the interpreter is reading it sees `" "` for one and `"\t"` for the other, so it throws up an error that different methods of indentation are being used for the same code block. – Matthew P. May 15 '20 at 20:19
  • Thank you guys. I'm using Notepad++ text editor and I found a function that makes all tabs into spaces and vice-versa, which is very helpful for this purpose. – Nikolay Tonev May 18 '20 at 01:35

0 Answers0