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 trialSaloni Mude
9,933 Pointsrandom.choice code challenge showing error even though the code returns the required result in the python IDLE ?
I've tested the code in the Python IDLE and it seems to work just fine , yet when i try to copy paste that code into the code challenge , it shows the error : "Bummer! Didn't get a correct option back from 'random_item'"
Am I missing something?
import random
def random_item (word):
#get random number between 0 and len of word
lenofword= len(word) - 1
randnum = random.randint(0, lenofword)
print ("The randomly selected number is {}".format(randnum))
wordlist = list(word)
print ( "The return value would be {}".format(wordlist[randnum]))
random_item("Treehouse")
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
2 Answers
Jennifer Nordell
Treehouse TeacherYes! And no. Your code is actually just fine. But keep in mind that just because it's functional outside the challenge doesn't mean that it meets the requirements of the challenge. Treehouse is going to call your function for you. So you can remove the call to the function that you've put in yourself. And if I change the last line of your code to return
the value, it passes! Because Treehouse explicitly asks you to return the result... not print it.
Take a look:
import random
def random_item (word):
#get random number between 0 and len of word
lenofword= len(word) - 1
randnum = random.randint(0, lenofword)
print ("The randomly selected number is {}".format(randnum))
wordlist = list(word)
return(wordlist[randnum])
Keep in mind, this is your code... just slightly altered
Jeremiah Bushau
24,061 PointsHe is asking for you to return the information rather than print it to the console.
return wordlist[randnum]
Saloni Mude
9,933 PointsThanks!
Saloni Mude
9,933 PointsSaloni Mude
9,933 PointsYeah I suspected a little that it might have something to do with return . Thanks a lot :D