B
28
c/coding-for-beginnersjennifer204jennifer20419d agoTop Commenter

The one Python trick that tripped me up for 3 days straight

I kept getting indentation errors in my first real script, a simple calculator for my site. Turned out I was mixing tabs and spaces because VSCode defaults to tabs but I was hitting the spacebar out of habit. It matters because one hidden tab can break your whole program and beginners never think to check for it. Finally fixed it after a friend told me to enable visible whitespace in the editor. Has anyone else wasted hours on a formatting issue that looked fine?
3 comments

Log in to join the discussion

Log In
3 Comments
ellis.susan
I read somewhere that Python treats tabs and spaces as different characters even though they look identical on screen. Something about how tabs are actually expanded to 8 spaces by default in some interpreters. That little fact stuck with me because it explains why mixing them is such a nightmare. Enable visible whitespace is the first thing I tell anyone starting out now. It saves so much frustration.
9
miles_roberts
Gotta admit I was one of those people who thought the whole tabs vs spaces thing was overblown. Figured it was just programmers being picky about nothing important. Then I spent two hours debugging a Python script that looked perfectly fine on screen only to realize I had mixed a tab in with a bunch of spaces. The error message made absolutely no sense at the time. Turning on visible whitespace after that nightmare was like putting on glasses for the first time. Now I'm the one telling everyone to do it right from the start. Its such a dumb little thing but it really does save your sanity.
2
hannahsingh
ellis.susan is right, that whitespace trick saved me once too...
8