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

George Lugo
seal-mask
.a{fill-rule:evenodd;}techdegree
George Lugo
Python Web Development Techdegree Student 922 Points

what is wrong with my code below

def random_item("Treehouse"): random.randint(0, len("Treehouse")-1) return(index."Treehouse"[0])

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"

def random_item("Treehouse"):
    random.randint(0, len("Treehouse")-1)
    return(index."Treehouse"[0])

2 Answers

Hello George,

The solution is as follows:

import random

def random_item(iterable):
    return iterable[random.randint(0, len(iterable) - 1)]

If you have any other questions I will update my answer, if you do not have any other questions:

Remember to upvote and to choose the best answer so that your question receives a checkmark in forums.

Kind regards,

Leo

Posting a solution without any comment is not very educational, and it might even be counterproductive IMHO. If you take the trouble to answer, then at least analyse the mistakes in the code, and give some instructions to the guy.

Gyorgy Andorka I have to disagree with you, when learning I find it much more useful to compare my code with better code. Some disagree, which is why I state that I am open to answer any other questions!

I know that George Lugo has learned about all of the syntax in my answer, I think it's better practice and creates a deeper understanding if the person asking the question has to do most of the research themself. An answer, IMHO, should provide a way to guide a person towards doing their own research!

Agree to disagree, and if there is a disagreement, I will take the effort to edit my original answer accordingly!

Kind regards,

Leo

Leonard Bode I understand your point, but in this case it's not just about "better code" - there are serious syntactical mistakes (indicating conceptional misunderstanings) in George's code which I think have to be addressed.

Gyorgy Andorka Fair enough, but the effort must at least be made to understand the proper syntax, given my code. If, after giving some effort, there are still things left not understood, I will provide more information accordingly.

Admittedly, my answer was quite lazy. In future I will provide bulletpoints or comments to help guide the person towards a better understanding.

Kind regards,

Leo

First, the function should work for any argument, not just the example string "Treehouse", so you should define a variable inside the braces (a parameter).** Now, random.randint(0, len(iterable) - 1) gives a random index from iterable, that's correct. You get an element out from an iterable (string, list, etc.) by using iterable[<index>]. Also, it's not necessary to use braces after return (but allowed).

def random_item(iterable):
    random_index = random.randint(0, len(iterable) - 1)
    # you could of course write return iterable[random.randint(0, len(iterable) - 1)],
    # without saving the random index to a variable first,
    # but it's cleaner and more readable this way    
    return iterable[random_index]

** Note: you can give a default value for this parameter by function(param=<default_value>), so when you call the function without passing in in any arguments, it will use the default argument, but that's another thing.