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 trialBrian Lee
1,569 PointsNot sure if I am creating this list correctly?
So this prompt asks me to create a new variable called colors with a list of 5 colors. The tutorial video beforehand only shows lists made with numbers.
Going off that, I first did the following:
colors = [red, yellow, green, blue, purple]
Then, I got the error message saying that 'red' was not defined.
Following that, I attached the following code:
list = [red, yellow, green, blue, purple] colors = list
Does anyone know how to solve this so that I can create a list with colors listed in words? I understand how to make lists with numbers, but not with words.
list = [red, yellow, green, blue, purple]
colors = list
2 Answers
Alex Arzamendi
10,042 PointsThe thing is, when you pass the code in such way, the python interpreter thinks that the colors defined inside the list are variable holders, meaning that it expects red, yellow etc to have values inside of them, if you want to pass them as strings then you would need to change your code to:
# Create a list with string values representing colors
list = ["red", "yellow", "green", "blue", "purple"]
# copies the stored list into a colors variable which will also hold the same information
colors = list
This is not only for strings, you could have defined red etc before and given them values and then you would be able to pass it to list without it telling you that they have not been defined. Try it!
jacinator
11,936 PointsYou need to create a list of strings and not a list of variables. What you are doing right now is telling it to create a list of the variable red
, the variable yellow
and so on. By putting quotes around them they become strings and it doesn't try to find them.
The other thing is that you don't need to assign the list to list
and then reassign it to colors
. You can directly assign it to colors
.
colors = ["red", "yellow", "green", "blue", "purple"]
Happy coding!
Brian Lee
1,569 PointsBrian Lee
1,569 PointsThanks, Alex! That works. I'm sometimes still not sure when and when not to use quotes in Python.