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 trialFerdous Hossain Fahim
687 PointsUnsure on what to do next
I'm stuck on this quiz. I don't know how to generate the random number which is minus 1 the length of the word and also how to print out the index related to the number in the word. Please help!
# EXAMPlE
import random
random_item("Overwatch")
random.randint(len(random_item)
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
2 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsThere are a few steps missing.
ā¢ you need to create a function named random_item that takes a single argument, an iterable. Remember to start with the def
statement
ā¢ To get the random number minus 1 add a "-1" inside your parents
random.randint(len(arg)-1)
ā¢ Remember to assign this to a variable so it can be used to grab the correct character.
ā¢ Finally use a return
statement either the proper result
Post back if you need more help. Good luck.
codeoverload
24,260 Pointsimport random # -> import the random library
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
# -- create random_item function --
def random_item(word):
random_index = random.randint(0, len(word)-1) # -> use the random.randint() function to get the index
return word[random_index] # -> return a random item by using your random index
# -- call the random_item function --
print(random_item("Treehouse")) # -> calls the function and immediately prints its output to the console