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

Christian Rowden
Christian Rowden
2,278 Points

Item.py hangup

I'm having a problem with this particular challenge. It doesn't seem clear to me. My code works but it seems to be returning the wrong number or something. Help and thanks?

import random

def random_item(witty): random_num = random.randint(0, len(witty) - 1) print(random_num)

random_item("Treehouse")

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

def random_item(witty):
    random_num = random.randint(0, len(witty) - 1)
    print(random_num)

random_item("Treehouse")

4 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are close, instead of returning the random number, you need to return the element at the index of the random number in the collection that is given as the argument to your function, just like in the example where the letter at index 4 in treehouse is h.

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

here you are just taking an integer and applying it as an index to an iterable to get the element at that index. python uses 0-based indexing, so the first item in a collection, or the first letter in a word, is at index 0. the last element in an iterable is therefore at the index number (length of iterable minus 1). so the word cat has length 3, and indices 0, 1 and 2, for c, a and t respectively. so you can see why in the word treehouse, the h is at index 4. in python the slice (portion of an iterable) operator is the square brackets []. the basic syntax is iterable[begin index : end index]. the slice returned will include the begin index but will not include the end index. so if we wanted to slice cat but leave off the t, we would do cat[0:2]. you can do a lot with slicing but here they are asking for the most basic slice - 1 element. you can just put the number of the index you need in the square brackets and it will return a slice of just that element. so from the example, treehouse[4] = h. you are calculating the random index correctly, now just use that in the brackets on the iterable to get the given element.

Christian Rowden
Christian Rowden
2,278 Points

With what you specified, it has brought me here:

import random

def random_item(witty):
    random_num = random.randint(0, len(witty) - 1)
    return witty.index(random_num)

random_item("Treehouse")

I'm still too new to programming to quite understand these errors but I'm working on it. So if I understand this correctly...python is trying to convert random_num into a string when it is being returned and can't? Thus throwing the TypeError?

Christian Rowden
Christian Rowden
2,278 Points

While I did already understand the concepts in your second reply, I also was reading everything incorrectly and trying to apply my flawed logic to it all. I've finally passed this challenge and in no small part because of your help. Thank you.