Python Object-Oriented Python Instant Objects Master Class

Prisca Egbua
Prisca Egbua
1,167 Points

This code looks okay but I get "TabError: inconsistent use of tabs and spaces in indentation"

The problem occurred when I try to add a new method run_lap that takes a length as an argument, reduces the fuel_remaining attribute by length multiply by 0.125, and increment lap attribute. Please help?

racecar.py
class RaceCar:
    def __init__(self,color,fuel_remaining,**kwargs):
        self.color=color
        self.fuel_remaining=fuel_remaining
        self.laps = 0
        for key,value in kwargs.items():
            setattr(self,key,value)

    def run_lap(self,length):
        self.length = length
        self.fuel_remaining = self.fuel_remaining - (self.length * 0.125)
        self.laps = self.laps + 1
Mariah Winifred Palangpour
Mariah Winifred Palangpour
11,795 Points

It would be awesome if the treehouse challenge workspace could adapt to allow you to set tabs to use four spaces, like you can do in other editors, and/or somehow highlight non-matching tab and spaces use. I agree that it is extremely frustrating when you've written solid code otherwise and can't see what you've done wrong. I've always used tab and it seems like the code challenge editor always uses 4 spaces when it indents for you after the creation of a block. I understand that spacing is super important in Python, but having to copy/paste code challenge code into workspaces or another editor just to figure out the otherwise non-visible difference between tabs and spaces is interrupting my ability to get through the Python courses with speed.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The error you are getting is indicative of mixing tabs and spaces in the same file, which you may not do in Python. You may not indent with tabs on one line and then indent with spaces on the next. This is built-in to Python and it's because Python is dependent upon whitespace to know what constitutes the beginning and ending of a block of code. Go back through your code and make sure you either use all tabs or all spaces for indentation.

Hope this helps! :sparkles: