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!

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 Python Basics Meet Python Create a Variable

I keep getting error message for this code. I don't understand what is missing. Thanks for the help.

The error mentions the output using_variables.py

(favorite_color)= "Blue"
print("The color", favorite_color, "is my favorite!")

Ok, just got the answer myself. I forgot scope...... ;-)

So of course inside functions they are needed too.

2 Answers

Louise St. Germain
Louise St. Germain
19,424 Points

Weird... this code works when I type this into the challenge.

That said, one quick note is that you don't need the parentheses around your variable name when you're defining it. You can just use:

favorite_color = "Blue"

If you're still getting an error, could you let me know exactly what the error message says?

Louise St. Germain
Louise St. Germain
19,424 Points

Hi Nils,

Haha, yes, I was surprised it worked too! But I tried it both in Workspaces and in VSCode and it worked in both places. I just tried the same sort of thing in JavaScript, now that you mention it, and it works there, too!

I think in general it's not good practice because it makes things more complicated than they need to be, but it doesn't seem to be an actual problem as far as getting the code to run.

Ah thanks, never would have thought this would work... Would not have even try it out but I believe you. :-)

Louise St. Germain
Louise St. Germain
19,424 Points

You should give it a try! Anyway, I guess we both learned something new today. :-)

Hm, I tried

var (favorite_color) = "blue";

in the google chrome js console, but it gives me a

Uncaught ReferenceError: favorite_color is not defined

Are you sure this works outside of certain editors?

Louise St. Germain
Louise St. Germain
19,424 Points

Hmm... yeah, good point. In the Chrome JS console:

// This won't work
var (favorite_color) = "blue";

// But this will...
(favorite_color) = "blue";

// And so will this:
var favorite_color = "blue";
(favorite_color) = "yellow";

So there is a bit of a grey zone - variable definition with "var" doesn't like it, but once it's defined, it seems not to care.

Hi Louise, not sure why it was giving me error message before but yes you're right interestingly it worked with and without the brackets this time. Thank for your help!

Wow, now this is really interesting news for me: We don't need var, let or const to introduce a variable in JavaScript?

I always thought you need a keyword in JavaScript when introducing a variable. So var, let or const are only needed when introducing a variable without declaring a value yet? Otherwhise I would see no sense in using var at all...

Why should one ever write

var myName = "Nils";


myName = "Nils";

works the same?

Update: Ok, just got the answer myself. I forgot scope...... ;-) So of course inside functions they are needed too.

Louise St. Germain
Louise St. Germain
19,424 Points

Hi Mauricio,

Glad it worked! :-)

And Nils (sorry Mauricio, there is a bit of a side discussion about both Python and JavaScript happening!) - yes, right - scope! Declaring a variable without var (or let or const) will work anywhere (even inside a function or whatever), but the scope will end up global. So it's definitely best to use var/let/const to avoid accidentally overwriting and/or creating global variables. But as far as syntax goes, JavaScript will accept a variable declaration without the var.

Hi Mauricio,

you actually cannot use brackets () around your variable name in python.

So you declare a variable like this:

text_variable = "test variable"
number_variable = 4

So you just have to delete the brackets around your variable name.

Happy coding!


PS: You can upvote my post and/or mark as "best answer" (at the bottom of my post) if it helped you. :-)

Hi Nils, It worked now. Thank you for the help .