What am I still doing wrong?
Please help this whole thing isn't working and its driving me crazy. Anyone who has tried to help before thanks I just still don't get it.
import random def random_item(pen): [ 'life' 'love' 'live' 'learn' 'eat' 'pray' 'enjoy' 'happy' ] n = random.randint(1,len(pen)) return pen[n-1] # EXAMPLE # random_item("Treehouse") # The randomly selected number is 4. # The return value would be "h"# EXAMPLE # random_item("Treehouse") # The randomly selected number is 4. # The return value would be "h"
Your code is incredibly close, the issue is the list you have written right after the function declaration. It does not belong there and makes your code invalid. If you remove it like this:
import random def random_item(pen): n = random.randint(1, len(pen)) return pen[n-1]
Then your code will work. Technically the task asks you to generate a number between 0 and the length of the iterable -1, so the intended solution is strictly speaking this code:
import random def random_item(pen): n = random.randint(0, len(pen) - 1) return pen[n]
But since you subtract 1 when referencing the index in your code it works the same as the intended solution.
Steven Parker171,137 Points
Instead of asking the same question over you could just post comments on a single question.
But I gave you some hints on the first one, and then a few more on the second one. Both times I said the most important hint was that you won't need to provide any data.. Then on the third question Tor showed the exact lines you are doing this on and said you don't need them. And those lines are still in the code, but the assignment part is missing so now they just cause a syntax error. Just remove that unneeded data and you'll be in good shape.
And when issues arise, you might also try looking at some of the many other questions and answers people have posted about the same challenge.