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 trialSharon Zonneveld
1,318 PointsI really don't get his question.
I really don't get his question. What do I need to do with my_list? I don't get the second and third sentence of the question. From the video I understand I need to print the range and use the numbers for it, but what about the append?
my_list = []
for a in range(0, 10):
print(a)
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsHey Sharon Zonneveld, letβs break it down:
- Write a for loop that iterates over a range with a stop value of 10 (assume the start value is 0 and the step value is 1). β
-
The body of the for loop should append the current value (your
a
variable) to the provided list called my_list. Alist
has an append. This modifies a list in place so no = assignment is needed. -
To append a value to a list, use the syntax my_list.append(val). is an example of how to use the method. In this case, the loop variable is
var
.
Post back if you need more help. Good luck!!!
Kristjan Vingel
7,992 PointsInstead of using the print() function, you need to use the append() method.
Right now, you're simply printing the current item to the console. However, instead of printing the item, you need to add it to the my_list - or append it (same thing). At the beginning my_list is empty, but once you run that for-loop with the append method inside, items that you're looping over, get added to the my_list one-by-one.
The 'item' in this case is a number - but can be also e.g. a string or another list.
Every list you declare has the append() method + tons of more! https://docs.python.org/3/tutorial/datastructures.html
Sharon Zonneveld
1,318 PointsSharon Zonneveld
1,318 PointsAfter trying a lot and searching on the internet and reading both answers on my question, I found out how to complete this. Thanks a lot for the answer!