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
170,249 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
170,249 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
170,249 Points

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

It worked!! thank you.