Python Python Basics (2015) Letter Game App Random Item

The random.randint() practice doesn't seem to work. It runs ok in IDLE, but always shows incorrect (Bummer) in Treehouse

import random def random_item(my_item): my_pos = random.randint(0,len(my_item)-1) return my_item[my_pos:my_pos+1] print(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(my_item):
    my_pos = random.randint(0,len(my_item)-1)
    return my_item[my_pos:my_pos+1]
print(random_item("Treehouse"))

1 Answer

Steven Parker
Steven Parker
173,989 Points

It might appear to work in the IDE because you're allowing it to return a list, or you're only testing with strings.

But the challenge expects the function to return a single member no matter what kind of iterable is given.

Hint: Try indexing instead of slicing.