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 trialNelson Stewart
64 PointsWhy 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])
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
markmneimneh
14,132 Pointsit 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"))
markmneimneh
14,132 PointsHello
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
Nelson Stewart
64 Pointsdef random_function(choice):
random_item = ("Treehouse")
arg1 = (len(random_item) - 1)
arg2 = random.randint(0, arg1)
random_item2 = list(random_item)
return(choice[arg2])
Nelson Stewart
64 PointsIts supposed to be a part of a function.