Python Python Basics (2015) Letter Game App Random Item

Stuck again on this random.randint exercise

Can any one help on this task, i've been stuck on this for the past hour.

Not sure if my code is even close to the solution.

item.py
import random

def random_item(name):
    letter = random.randint(0,len(name)-1)

    return letter(name)
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! You're pretty close. You've chosen (for some reason) to assign an integer to the variable named letter. The iterable being passed in you've chosen to name name. If we then want to get the value at the position (the number named letter) from name and return it, we would do it like this:

 return name[letter]

So if your function got the random integer 4 and we passed in the word "Treehouse", your code would return "h". That is the value at the index of 4 in the string.

Hope this helps! :sparkles:

Hi Jennifer

WOW your advise really cleared it for me. I'm not taking Treehouse everyday since i'm already working Full Time i'm completing the courses at my own pace. So i tend to forget alot of useful things in Python. Thank you so much.

Michael Hulet
Michael Hulet
Treehouse Moderator 46,738 Points

I upvoted to even it out, and I also marked this answer as best because OP says it fixed their problem