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 trialAli Al Dairawi
Courses Plus Student 877 PointsStuck 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.
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
Treehouse TeacherHi 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!
Ali Al Dairawi
Courses Plus Student 877 PointsAli Al Dairawi
Courses Plus Student 877 PointsHi 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.
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsWhy was this downvoted?
Michael Hulet
47,913 PointsMichael Hulet
47,913 PointsI upvoted to even it out, and I also marked this answer as best because OP says it fixed their problem