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

Chinmay Tare
Chinmay Tare
467 Points

Please help friends :/

Stuck.

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random 
def random_item("number"):
  item = random.randint(0, 5)
  for letter in random_item:

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Chinmay,

randint method will pick a random number between 0 and the length of the methods argument. But donΒ΄t forget to substract 1 from the length of the argument. If you have a string argument "Treehouse" 'T' is on index 0 and the last 'e' is on index (length - 1).

import random 

def random_item(iterable_item):
  item = random.randint(0, len(iterable_item) - 1)

  return iterable_item[item]

Makes sense?

Grigorij

Steven Parker
Steven Parker
231,007 Points

Here's a few hints:

  • for the function definition, don't enclose the name of your argument in quotes
  • the upper limit of randint should be one less than the length of the argument (not "5")
  • as soon as you get the item, just return the argument, indexed by item
  • you won't need a loop

I'm guessing you can get it now without an explicit spoiler. You might also want to review the video.