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 trialNikhil Alexander
1,444 Pointsthis code does seem to pass the challenge
im not sure whether to subtract one from the length of the iterable or from the random number.. if im making some other mistake please help me correct it
import random
random_num = random.randint(0, 7)
def random_item(Avengers):
return(Avengers[random_num])
2 Answers
Steven Parker
231,236 PointsThe random number should be selected inside the function, and the range should be based on the size of the argument instead of any fixed value.
Dan Garrison
22,457 PointsThe code challenge is asking you to give a random number between 0 and the length of the iterable provided as the argument minus 1. To do this you need to use the built in python function, len() to determine the length of the incoming argument. Remember, functions are reusable code snippits. They are useful because you don't always know what the input will be. In this case they could give you an iterable (string, list, etc) that is 10 characters in length and another time you could get an iterable that 15 characters in length. Your function should be capable of handling both.
You should not be declaring any variables outside your function. In fact, you can even complete the challenge without declaring a variable.
Nikhil Alexander
1,444 Pointsthank you.....i understood the concept
Nikhil Alexander
1,444 PointsNikhil Alexander
1,444 Pointsthank you so much the code passes thank you again for your help!!