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 trialAlex Ojelade
748 PointsDont understand this challenge
Dont understand this challenge, i have given it a try but i don't know how close i am to being correct and am stuck at the return part.
import random
stuff = ['computer', 'screen', 'mouse', 'keyboard']
def random_item(stuff)
it_num = random.randint(0, len(stuff) - 1)
item = random.choice(stuff)
return index()
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
1 Answer
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 PointsTo start, you dont need to define 'stuff'.
At the end of the def line, you are missing the colon:
You then just need to return the item at the random position.
import random
def random_item(stuff):
it_num = random.randint(0, len(stuff) - 1)
return stuff[it_num]
or in one line:
import random
def random_item(stuff):
return stuff[random.randint(0, len(stuff) - 1)]
Alex Ojelade
748 PointsAlex Ojelade
748 Pointsthanks although....
I had your first one before at one point but probably missed the colon after 'def random_item(stuff)'. So i reverted back to that. However, it seems that treehouse says it doesn't work.
Wierd because i tested it in Shell and it worked