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

Need a little help with my code

Hello, With regards to the question i cannot figure out why my code doesn't work. i'll glad if some explanations is attached.

Thanks!!!

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

import random

random_item(species):
    random_index = random.randint(0,len(species)-1)
    return (random_index)
random_item(species)
Chastin Davis
Chastin Davis
Courses Plus Student 2,299 Points

I tried to use your code .. From what you have here. you need to make sure you use def for all methods/ functions you create. Then, take away the ( ) around species. Just the syntax as follows; return species. Next, define species (i.e. species = "species" ). Also the challenges on Treehouse usually do test in the background so you can also do a def random_item("species"), I think. Lastly, try out your potential answers in the workspaces. That's how I get pass some of these task. I also ask around too. By the way you want to give the letter in the item. So far your method is giving the index.

3 Answers

Cody Te Awa
Cody Te Awa
8,820 Points

Hey Eugene, It appears you just haven't put the keyword 'def' before your function. Try this:

def random_item(species):
    random_index = random.randint(0,len(species)-1)
    return (random_index)

EXAMPLE

random_item("Treehouse")

The randomly selected number is 4.

The return value would be "h"

Remember you have to return the index letter (which is the random_index) like return species[random_index]

Thanks very much f