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 trialFerdous Hossain Fahim
687 PointsCan't figure out what I'm doing wrong
I'm not sure how exactly to return the character of the index, if this is the way im not sure what im doing wrong. Please help
# EXAMPLE
def random_item('bar'):
index= random.randint(0,len(bar)-1)
return index
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
1 Answer
Charles Kenney
15,604 PointsSure, Ferdous.
Firstly, you want to make sure you import the necessary library or module directly before you call it in your function.
import random
or:
from random import randint
Secondly, instead of returning the random index, you need to return the character of the string ('foo' in this case) at that random index! We can do this by subscripting the string passed through the function (bar).
example:
character = string[index]
in this case we need to:
from random import randint
def random_item(bar):
index = randint(0, len(bar) - 1)
return bar[index]
Hope this helps! -Charles