Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Basic Object-Oriented Python Welcome to OOP Methods

Why does my code run fine in workspaces but I get a SyntaxError on VS Code?

class Car:
    wheels = 4
    doors = 2
    engine = True

    def __init__(self, make, model, year=2021):
        self.make = make
        self.model = model
        self.year = year
    def stop(self):
        print("The car has stopoped")
    def go(self, speed):
        print(f'The car is going {speed}')

car_one = Car("Ford", "Model T")
car_two = Car("Model X", 2021)
car_one.stop()
car_one.go('fast')

ROBERTOs-MBP:~ roberto$ python -u "/Users/roberto/Downloads/cars.py" File "/Users/roberto/Downloads/cars.py", line 13 print(f'The car is going {speed}') ^ SyntaxError: invalid syntax

1 Answer

Hi Roberto,

In workspaces python references some (the latest???) version of Python 3 (Python 3 is when f strings were introduced into the language).

I imagine that in VS Code/on your local machine, python references Python 2. Assuming you have already installed Python 3 onto your machine, try the following in your terminal.

ROBERTOs-MBP:~ roberto$ python3 -u "/Users/roberto/Downloads/cars.py"

If you haven’t installed Python 3 yet, you’ll need to do that first.

Hopefully, that’s work out for you. Good luck.

Thanks, this worked!