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 trialor porat
684 PointsReally need help in this one
Hey! Im really trying over and over this one but i can't figure out what to do or what to return. ill be glad to get help thanx!
import random
def random_item(apple):
random.randint(3)
return apple
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
2 Answers
Umesh Ravji
42,386 PointsThe random_item function takes a string and returns one of the characters in the string at random.
First to generate the random number, you have to find the max to supply to the randint function.
len(apple) - 1
You can obtain a random index using randint:
index = random.randint(0, len(apple) - 1)
Using the randomly generated index number you can return the character at that index, which I leave to you :)
Alexander Davison
65,469 Pointsimport random
def random_item(items):
return items[random.randint(0, len(items) - 1)]
or porat
684 Pointsthanx alex!
or porat
684 Pointsor porat
684 Pointsthanx umesh!