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 trialSky Lu
1,260 PointsRandom Task Can't Pass
below is my code, and the system shown:"Bummer! Didn't get a correct option back from random_item
"
I ran my codes works well. I don't get the problem
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
word = 'apple'
def random_item(word):
length = len(word) - 1
num = random.randint(0, length)
word[num]
random_item(word)
3 Answers
Stuart Wright
41,120 PointsYou missed that all-import return keyword at the end of your function. Your function currently doesn't return anything.
import random
def random_item(word):
length = len(word) - 1
num = random.randint(0, length)
return word[num]
Stuart Wright
41,120 PointsThat doesn't work because you print w instead of return it. If you replace your print(w) line with return w then it works perfectly.
someguy1234
3,450 PointsThank you, guys for all the inspiration. I looked at old notes and use index! Here is my code:
import random
word = "Treehouse"
def random_item(word):
return word[random.randint(0, len(word)-1)]
Sky Lu
1,260 PointsSky Lu
1,260 PointsHi Stuart, Thanks for your reply, how about I did as below: