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

Stuck for days on this one, where am i going wrong

the whole question for the task is below

I want you to try and reproduce it yourself.

First, import the random library.

Then create a function named random_item that takes a single argument, an iterable. Then use random.randint() to get a random number between 0 and the length of the iterable, minus one. Return the iterable member that's at your random number's index.

Check the file for an example.

item.py
import random
def random_item(iterable)
     random_number = random.randint(0, len(list(iterable))-1)
     return iterable[random_number]

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Victor;

A couple of things here.

1) You are missing a colon after your def statement.
2) Can you tell me why you are converting your iterable to a list?

Ken

The colon obviously is a problem, but I don't see why he shouldn't convert iterable to a list. Even after converting it to a list, it would return the same length. It only wouldn't work if the iterable is a dictionary, but that is unlikely because it doesn't make sense to "return a random value" from a dictionary. Is the value a key? Or is it a value? :/

Ken Alger
Ken Alger
Treehouse Teacher

Sometimes the challenge checking engine is pretty picky about what it is looking for and submitting code that "works" doesn't pass.

Oh. I see. :laughing:

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Regarding the use of list(). It is necessary in the case of a dict. All iterables should return a valid value in response to len(). Sets, strings, tuples, list, and even dicts, all have lengths. For dicts, the length is the number of keys. Unfortunately, a random number in the range of the length is unlikely to be a valid key when trying to return a value. So by converting the keys to a list, passing a dict to the function still works to return a random key.

That said, I don't believe the checker tests with a dict argument.

I would submit that if one's code meets the description of the challenge but doesn't pass then either the challenge description needs refining or the checker needs updating. We have recently seen valid code fail a different challenge. In that case the checker needed updating.

Nobody should have to guess at how the checker might misinterpret their code.

In this case, having code that's more robust than the checker requires seems fine.

Great job Victor!