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

I was able to get the code worked in workspace. However, the exercise here is telling its incorrect.

The below code executes fine in Workspace. The same isn't working in exercise. Thanks!

import random

def random_item(arg1):
    rand_num = random.randint(0,(len(arg1)-1))
    print("The randomly selected number is {}".format(rand_num))
    arg1_list = list(arg1)

    for letter in arg1_list:
        if arg1_list.index(letter) == rand_num:
            print("The return value would be {}".format(letter))
            break

random_item("Treehouse")

2 Answers

Steven Parker
Steven Parker
231,248 Points

This code may not cause errors, but what it does is different from what the challenge instructions ask for:

  • the challenge does not ask you to "print" anything
  • the challenge does not require formatting strings
  • the challenge does not require a loop
  • the challenge does ask that the function return a single member based on a random index

Thank you! I think i looked at the example given in "gray" and deluded. I will correct the code and retry. Thanks again!

Steven Parker - Thank you! I've corrected the code and got through the challenge.