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

Please explain what I'm doing wrong?

I am not really sure what I am doing right or wrong I haven't been coding for 14 days oops. Please help.

item.py
import random 

def random_item[
'life'
'love'
'live'
'learn'
'eat'
'pray'
'enjoy'
'happy']:
    random.randint(0 < 8)
     random.randict - 1
        return random.randict
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"

2 Answers

Steven Parker
Steven Parker
231,269 Points

Here's a few hints:

  • when defining a function, the name must be followed by parentheses with the parameter(s) inside
  • you won't need to provide any data, the challenge will do that itself
  • the "randint" function takes two arguments separated by a comma
  • you'll need to use the length of the argument to get a random number in the right range
  • the "random" class has no "randict" property
  • the return value needs to be selected from the incoming argument

Hopefully this breaks down the steps for you

  1. random_item function needs an argument for iterable. syntax looks like: def func_name(arg):
  2. need to evaluate how many items are in this iterable to set the range for randomization. len(arg)
  3. determine random number. n=random.randint(1, len(arg))
  4. return the result. return arg[n-1]