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

Why doesnt this work?

import random random_item = ("Treehouse") arg1 = (len(random_item) - 1) arg2 = random.randint(0, arg1) random_item2 = list(random_item) return(random_item2[arg2])

item.py
import random
random_item = ("Treehouse")
arg1 = (len(random_item) - 1)
arg2 = random.randint(0, arg1)
random_item2 = list(random_item)
return(random_item2[arg2])

4 Answers

it looks like you are trying to return a char at random. This is a one line example that I hope will point you in the right direction

import random

def random_function(choice):

return(choice[random.randint(0, len(choice)-1)])

print(random_function("Treehouse"))

Hello

Your code won't work because of the return statement. A return is something you use in a method

do you mean something like this instead?

import random

def a_method():

random_item = ("Treehouse")

arg1 = (len(random_item) - 1)

arg2 = random.randint(0, arg1)

random_item2 = list(random_item)

return(random_item2[arg2])

print(a_method())

if this answers your question, please mark the question as answered

thanks

def random_function(choice):
    random_item = ("Treehouse")
    arg1 = (len(random_item) - 1)
    arg2 = random.randint(0, arg1)
    random_item2 = list(random_item)
    return(choice[arg2])

Its supposed to be a part of a function.