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

Idan shami
Idan shami
13,251 Points

help random item challenge

I don't know what to do... please help.

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(arg):
    arg = random.randint(len(arg)) -1
    return arg

2 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya! You are close but there are a couple issues with code, and this might help ya a bit:

import random 

def random_item(words):
    num = random.randint(0, len(words)-1)
    return words[num]
Idan shami
Idan shami
13,251 Points

thanks ! it worked! but I don't understand what the ( ) after the random.randint, I do understand len(words)-1) but what is the 0 does? and why the -1 isn't outside the ( )?

thanks a ton!

Idan

Ari Misha
Ari Misha
19,323 Points

Idan , if you look at docs for "random.randint()" module, "randint()" takes two arguments a and b. Where a is the starting point and b is the end. Hence, randint will randomly select an integer between "a" and "b". In case , if ya wont provide any arguments, "randint" will randomly select an integer between 0 and 1 for ya. So yeah to override the arguments a and b in randint, as per instructions given in the challenge, the range was given to randint was "0" and "len(words)-1". I hope it helped! (:

Idan shami
Idan shami
13,251 Points

it was helpful! thanks