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 trialJohn Wetherbee
1,467 PointsI don't know where to begin on this objective. Please help. Thank you! John
This is too hard for me.
I would appreciate if someone helped me.
John
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(iterable):
random.randint(0, 10)
5 Answers
nicole lumpkin
Courses Plus Student 5,328 PointsHey John,
You got this! Let's break this down :)
You've seen how random.choice() works. It gets a random member from an iterable (like a list or a string). I want you to try and reproduce it yourself. First, import the random library.
import random
Then create a function named random_item that takes a single argument, an iterable.
def random_item(some_iterable):
Then use random.randint() to get a random number between 0 and the length of the iterable, minus one.
index = random.randint(0,len(some_iterable)-1)
Return the iterable member that's at your random number's index.
return some_iterable[index]
So the whole thing amounts to this:
import random
def random_item(some_iterable):
index = random.randint(0,len(some_iterable)-1)
return some_iterable[index]
John Wetherbee
1,467 PointsThanks vey much Nicole! Haven't a clue how this one would be solved by a beginner like myself.
Have a great Sunday!
John
nicole lumpkin
Courses Plus Student 5,328 PointsDon't be discouraged!! I'm a beginner too, I'm just further along in the lessons than you are. I feel lost, confused, and frustrated many times a week. I've even wept at my computer a time or two ;) But if you love this stuff and if you keep chipping away you will get it! I've been a personal trainer for the past six years, and prior to that a waitress for 5 years. I recently quit my successful training job and I'm teaching myself Python full time. If I can do this so can you :) I'd wish you good luck but that's not what it's about. So have a good time studying and experimenting every day!!! Also, perhaps start doing what I'm doing. Go into the community forums and scroll for unanswered questions from fellow students that you can answer! If you can explain it to someone else it helps them as well as yourself. Happy Coding <3
John Wetherbee
1,467 PointsThanks for your response Nicole!
Keep in touch!
John
nicole lumpkin
Courses Plus Student 5,328 PointsHi John, How are things :)??
Sashi Shah
2,528 PointsThanks Nicole..those are encouraging words. :)
John Wetherbee
1,467 PointsHi Nicole,
Things are well, thank you. Been studying off a book and not listened to Treehouse for a week now. But I plan to resume Treehouse training soon. My email is johnwetherbee3@gmail.com. I would like to communicate but in a more private medium.
Thank you for reaching out to me!
John