## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today. PLUS

# random

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h" It doesn't look like you've written any code yet. There's a partial example in the comments. Plus the challenge tells you what function you need to call to get a random number. Surely you can get at least that far.

Please give it your best "good faith" attempt, and then post your code if you still need some help.

If you're totally lost, it might be a good time to go back and review some of the videos.

Steven Parker, duly noted.

Hello

the challenge is asking the following:
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"

so you have a string "Treehouse"  which nothing more than an iterable of chars.  To prove thsi:

for letter in "Treehouse":
print(letter)

will produce
T
r
e
e
h
o
u
s
e

the length of the string is len("Treehouse")
print(len("Treehouse")) should result in 9

so now the challenge is asking you to create a function named  random_item which you are going to pass a string as a param

def random_item(string):

in this function you want to grab a random number between 1 and the length of the length of the string

this should do it
print(random.randint(1,len(string)))

therefore, so far we have

def random_item(string):
a_random = random.randint(1,len(string))

we now just need to return the letter at position a_random

this would be return string[a_random]

so now now we have a full function

def random_item(string):
a_random = random.randint(1,len(string))
return string[a_random]

and to call it, I would do this

a_letter = random_item("TennesseeVolsAreGreat")
print(a_letter)

There you got it.

I hope that by walking you through the thought process, this will help you break down a larger problem into smaller logical steps through which you can solve much original problem.

please tweak the example I provided so meet the challenge requirements.   For example, the challenge is expecting you to print from inside the function.

good luck


There is one issue with this code:

a_random = random.randint(0, len(string)-1)


This will give a random number between 0 and the number of indices in the iterable.

a_random = random.randint(1, len(string))


This will give a random number between 1 and the number of items in the iterable. With this code you will never get the first item in the iterable and you will possibly get an error for an index being out of range.

hi ...

@Dustin James ... yes, it should be 0 , not 1... thanks for catching.

@Steven Parker ... yes, but as a walk through on problem solving ... not a bock of run'able code ... please read the initial request.

thanks