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 trialSamson Chapuramhuka
7,390 PointsWrite 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
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 to the provided list called my_list. To append a value to a list, use the syntax my_list.append(val).
my_list = range[10]
for val in my_list:
my_list.append(val)
return val
5 Answers
Josh Keenan
20,315 PointsYou don't need a return statement, a return statement is only used within functions or methods.
my_list = []
for i in range(10):
my_list.append(i)
This is my solution, it iterates 10 times and adds the current loop count to the list.
jon nikolakakis
1,639 PointsThanks Josh Since question asked for val, it could also look like this. With 0, starting the range at 0 and increment to 1 Or using default
my_list = []
for val in range(0,10,1):
my_list.append(val)
or
my_list = []
for val in range(10):
my_list.append(val)
Stanley Nkosi
4,233 PointsJosh Keenan your answer was the best.
Josh Keenan
20,315 PointsYou can click the best answer button to mark it as such, and glad I could help. You got this!
Stanley Nkosi
4,233 PointsJosh your solution was the best answer.
Likhitha Reddy
7,623 Pointsmy_list=[] for my_list in range(0,10,1) my_list.append(val)
Stanley Nkosi
4,233 PointsStanley Nkosi
4,233 PointsThank you very much Josh.Your solution was spot on ! I can go to sleep peacefully tonight.
Lynn Collins
10,080 PointsLynn Collins
10,080 PointsTried it first, then it gave me a red message. Discovered that I needed to keep those brackets empty. Thank you Josh, this passes the challenge very well :)