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

Jerle Norton
Jerle Norton
905 Points

I am having trouble with another exercise. The solution I came up with works in pycharm but won't work here.

https://teamtreehouse.com/library/python-basics/letter-game-app/random-item

My solution: import random

def random_item(i): x = random.randint(0, len(i)-1) print(x) i = str(i) print(i[x])

random_item('treehouse')

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

1 Answer

jag
jag
18,266 Points
random.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random

def random_item(i): 
    x = random.randint(0, len(i)-1)
    i = str(i)
    return(i[x])

random_item('treehouse')

You need to return the value not just print it.

Jerle Norton
Jerle Norton
905 Points

I tried to return the value only, as it is in the screenshot above, and that too didn't work.

Jerle Norton
Jerle Norton
905 Points

I'm sorry but your suggestion did work. I just had to change my random_item() argument word. Thanks for the help.