Ali Dahud3,459 Points
It’s asking from me the impossible.
What should I do?
def random_item(string): random_num= random.randint(0,len(string)-1) return random.choice(random_num) # random_item("Treehouse") # The randomly selected number is 4. # The return value would be "h"
Alex KoumparosPython Web Development Treehouse Moderator 32,453 Points
This challenge isn't impossible, people (including, shortly, you!) pass this every day. I say this only as a reminder that all of these challenges, even the hardest ones, don't seem quite so daunting when you realise that they can be done. And usually (like in this case) you only need a little change to get it done.
In fact, in this case you only need to make a couple of small changes to your code to pass the challenge.
Firstly, you need to
import random. The challenge tells you to do exactly that: it's always a good idea to reread the challenge text in case there are any instructions or hints you're missing. Sometimes during the stress of a difficult challenge we don't pick up on all the instructions or hints during the first (or even second) read through, so it's always worth checking again.
Next, what you are trying to accomplish is to duplicate the functionality of random.choice, which means we can't use it. So for the last line of the code, to what would we need to apply the random number we got from the previous line of the code in order to get a value that represents a random choice from the iterable we passed into the function? In this case it might have been easier to spot if you had picked a different argument name for your function (this is something we get better at with experience, but is always hard. It has been famously observed that there are only two problems in computer science, and one of them is naming things). If you had chosen something like 'itr' as your argument name, to remind yourself that you are ultimately trying to pick an indexed value out of the iterator, you might have realised that for the last line we want to pick the nth value (which we got from the previous line) from the iterator:
Hope this helps, Cheers