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 trial
SOu TO
776 PointsWhat is the probleme in my random ?
import random
def random_item(single):
random_number = random.randint(0, len(single) - 1)
index = random.randint(0, len(single) - 1)
print ("The randomly selected number is {}".format(random_number))
print ("The return value would be {}".format(index))
5 Answers
Dustin James
11,364 PointsYou are getting two random numbers and storing both of them separately. To print / return an item at a specified index do this:
single[0] # put the number of the index you want to return in brackets. In this case the first item will be returned
single[random_number] # here we return the random index
# for example your last line would be
print("The return value would be {}.".format(single[random_number]))
SOu TO
776 PointsThanks !
SOu TO
776 Pointsimport random
def random_item(single):
# put the number of the index you want to return in brackets. In this case the first item will be returned
single[0]
random_number = random.randint(0, len(single) - 1)
print ("The randomly selected number is {}".format(random_number))
# for example your last line would be
print("The return value would be {}.".format(single[random_number]))
i finish the code like that it is good ?
Dustin James
11,364 PointsYour code will work but there are a few extras that are not needed. When I put the comment in about the index in brackets it was to serve as a reminder not that it is needed for this code.
import random
def random_item(item):
random_index = random.randint(0, len(item)-1)
return item[random_index]
Dustin James
11,364 PointsIf I gave you a list named numbers and it looked like this: [5, 3, 1, 7, 9] how would you print out the 3rd item in the list?
numbers[2]
What if I had a list named favorite_teachers and it looked like this ["Kenneth Love", "Pasan Premaratne"] and I asked you to pick a random teacher from the list. How would you do that?
# multiple choice
# A:
random_teacher = random.randint(0, len(favorite_teachers))
# B:
random_teacher = random.randint(0, len(favorite_teachers) - 1))
# C:
random_teacher = random.randint(1, 2)
# D:
random_teacher = favorite_teacher[2]
SOu TO
776 Pointsi will say
# A:
random_teacher = random.randint(0, len(favorite_teachers))
Dustin James
11,364 PointsSOu TO,
Let's break that answer down.
len(favorite_teachers)
This will return 2 because there are two teachers in the list of favorite_teachers. This means our random number generator will generate a 0, 1, or a 2. This is like me saying pick a number from 0 to 2.
Let's say the computer picks 0, which teacher do you think that is?
favorite_teachers[0] # this would return 'Kenneth Love'
What if the computer picks 1?
favorite_teachers[1] # this would return 'Pasan Premaratne'
I hope this is making since. But what happens if the computer picks 2? There isn't a teacher at index 2. For this reason, the answer is:
# B:
random_teacher = random.randint(0, len(favorite_teachers) - 1))
If you'll notice the -1 part here:
len(favorite_teachers) - 1
This looks like this: Count how many items are in favorite_teachers and subtract one from that. Why do we do this? This is because computers start counting from 0. That means the first item in an iterable is at index 0 and in the example of favorite_teachers there are only two items. The item at index 0 and the item at index 1.
I hope this helps. Let me know if you have any questions.
SOu TO
776 Pointsthanxs, i undertand better, "len" does not count as the index, the "len" begin at 1, while the "index" begin at 0. is that right ?