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

Windy Elliott
Windy Elliott
776 Points

random letter from string

This works in workspace just fine. I can print out the arg, print out the lenarg, and print out the letter and they are all coming out correctly. But for some reason it won't take it as an answer on the challenge.

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(arg):
    lenarg = len(arg) - 1
    randarg = random.randint(0, lenarg)
    letter = arg[randarg]
    return letter

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hi WIndy,

I think it is overcomplicating it, your answer is correct, just need to minify the answer a bit and it passes fine:

import random
def random_item(arg):
    randarg = random.randint(0, len(arg) - 1)
    return arg[randarg]