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

or porat
or porat
684 Points

'random.randit()' problem

Hey! I think i didn't understand 100% this question. the 'len' of apple minus one is 3, so i set number 0,3. what exaxly i need to return?

thanx!

item.py
import random

def random_item("apple"):
    random.randit(0,3)
    return "apple"



# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"

1 Answer

Ryan S
Ryan S
27,276 Points

Hi or porat,

The example text in the code challenge is just there to show you what the expected output should be if the function argument happened to be "Treehouse". They don't actually want you to input a string, nor do they want you to preset the limits of the random integer.

1) Your function should take one argument, you can name it whatever you want. The important thing is that you don't know what it will actually be when the function is tested in the code checker. All you know is that it is iterable, like a string or a list.

2) Figure out the length of the iterable argument.

3) Get a random number between 0 and the length of the argument minus 1. Note that you have a typo in random.randint(). You forgot the "n".

4) Whatever that random number turns out to be, that will be used as an index value. You then need to return whatever member (letter or list item) happens to be at that index in the iterable.

Hope this helps get you started.

or porat
or porat
684 Points

thanx ryan!