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 trial

Python Python Basics (2015) Letter Game App Random Item

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

Runs in workspaces but not here - random_item function

import random

fruits = ['apple', 'pear', 'banana', 'blueberry', 'plum', 'peach', 'lemon', 'orange', 'pomegranite']

#print ("fruits[2]:" , fruits[2])

def random_item(iterable):
   num = random.randint(1, len(iterable) - 1)

   return iterable[num]

print ("random_item[fruits}: ", random_item(fruits))

The above code runs in workspaces and produces a sensible answer. But I can't get past the code challenge and the feedback from the autograder is really not helpful. First it was just "bummer" and now it doesn't recognize random_item. I feel like I am really close on this, any help would be appreciated.

Nancy

2 Answers

Nathan Tallack
Nathan Tallack
22,160 Points

You were VERY VERY close. ;)

Consider the code below with the two changes commented:

import random

fruits = ['apple', 'pear', 'banana', 'blueberry', 'plum', 'peach', 'lemon', 'orange', 'pomegranite']

#print ("fruits[2]:" , fruits[2])

def random_item(iterable):
   num = random.randint(0, len(iterable) - 1)  # A 0 here not a 1
   return iterable[num]

print ("random_item[fruits]: ", random_item(fruits))  # A ] after first fruits not a }
Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

I think I can do it now. Thanks for your patient help. N