Tumultuous TabErrors: Developing Good Python Code Entry Habits

Hello everyone,

I have been working with python now for a few days now and am moving at a decent clip. One of the things that has been constantly tripping me up are inconsistent tabs, spaces, and other such errors in indentation.

Coming from Java, I understand the importance of keeping things consistent since I (thankfully) no longer need to sprinkle semicolons everywhere.

However, I think I might have met my match with this lesson:
https://www.codecademy.com/courses/learn-python-3/lessons/python-control-flow/exercises/boolean-operators-not/

The code I entered is:

def graduation_reqs(gpa, credits):
  if (gpa >= 2.0) and (credits >= 120):
    return "You meet the requirements to graduate!"
  if (gpa >= 2.0) and not (credits >= 120):
    return "You do not have enough credits to graduate."
	if not (gpa >= 2.0) and (credits >= 120):
    return "Your GPA is not high enough to graduate."
	if not (gpa >= 2.0) and not (credits >= 120):
    return "You do not meet either requirement to graduate!"

After fiddling with my code and eventually giving up, I decided to look at the correct answer which is:

def graduation_reqs(gpa, credits):
  if (gpa >= 2.0) and (credits >= 120):
    return "You meet the requirements to graduate!"
  if (gpa >= 2.0) and not (credits >= 120):
    return "You do not have enough credits to graduate."
  if not (gpa >= 2.0) and (credits >= 120):
    return "Your GPA is not high enough to graduate."
  if not (gpa >= 2.0) and not (credits >= 120):
    return "You do not meet either requirement to graduate!"

Normally, I’ve been able to figure out my tab and spacing issues by comparing each line but this time I don’t see any differences between my spaces, tabs, and indentation and sadly have met my match.

First, how do I resolve my tab errors in this lesson and secondly what are some good keyboard entry habits that I can do to ensure that my entries are formatted the way the compiler expects?

I appreciate any help and advice given. Thank you!

Edit: so after putting my code into this forum post, I can see the issue clear as day. Why does it look different here then in the lesson?

Here is a screenshot of the same code that I see on my web browser:

So I ultimately was able to fix the taberror by clearing out each “if not” statement after line 10 and using the space bar to match the indentation.

Before when I finished my “if statements” and was ready to type in the return, I would hit enter and it appeared to look ok so I moved on.

So this has now turned into my second question: what are good entry habits to get into the ensure I don’t spend 15 minutes fiddling with spacing and indentation?

2 Likes

Close-up screenshot of my code editor:

Do you see the grey marks (dots and arrows) to the left of each line of code? This feature is called “whitespace rendering”, every dot is a space, every arrow is a tab.

After using whitespace rendering for a few days you will be able to quickly notice mixed tabs and spaces, even if the indentation level looks ok.


Unfortunately, Codecademy editor does not allow you to turn on whitespace rendering, but the feature request was submitted.

2 Likes

Your editor shouldn’t be inserting mixed whitespace, so that’s a misconfiguration.
Tabs are complicated characters and I don’t see what value they add. Editors can jump between indentation levels and insert groups of spaces, better to insert the same as you see.

out

2 Likes

Thanks for the replies. I’m simply using the built in editor via the codeacademy website.

I’ve since opened up Pycharm Community and am using the IDE alongside the codeacademy website for entry to see if my syntax is correct.

My goodness, it’s a night and day difference. I can see now where my indents are mucked up.

I think this issue might be more of a quirk with the codeacademy website . Regardless, i’m back up to my fast pace again. Thank you everyone!

2 Likes

The short answer is “use spaces”. :slight_smile:

4 Likes

thepitycoder is essentially the solution.

However, the extended solution I’d also like to add that there is value by copying ones code from the website editor into an IDE like say Pycharm Community.

This way, it will tell you where the errors are exactly and how to fix them which then gives you that deeply satisfying green checkmark when you copy it back into the website.

Thank you everyone!

1 Like