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

You've seen how random.choice() works. It gets a random member from an iterable (like a list or a string). I want you t

i need some guidance in here. Please

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

def random_item("table"):
    random_number = random.randit(len("table") -1)
        return random_number

3 Answers

Steven Parker
Steven Parker
231,275 Points

Here's a few hints:

  • variable names (and function parameter names) should not be enclosed in quotes
  • the random method name is "randint" (with two "n"s)
  • the "randint" method requires two arguments, a lower limit and an upper limit
  • check indentation

import random

def random_item(table): random_number = random.randint(0,len("table") -1): return random_number

Here is the corrections i've made

Steven Parker
Steven Parker
231,275 Points

Better, but:

  • you still have quotes around a variable name
  • there should not be a colon at the end of the assignment
  • to get a member at your a specific index, put the variable name first, then the index inside brackets

import random

def random_item(table): random_number = random.randint(0,len(table) -1) return table<random_number>

here is more correction

Steven Parker
Steven Parker
231,275 Points

Perhaps I should have said square brackets. :point_right:   []

It worked!! thank you.