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

Logan Valdez
PLUS
Logan Valdez
Courses Plus Student 6,741 Points

Not Quite Sure What To Do. (Letter Game App challenge)

Hello. With this challenge I am supposed to return the iterable member that is at my random number’s index, however I am receiving an error with my code and I am not sure how to remedy this issue.

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

import random

def random_item(a):
    random.randint(0, len(a)) - 1
    return a

2 Answers

There are two things you need to fix to get this challenge working. The first is that the item you are getting in the function 'a' is the same thing you are returning. You have not done anything to 'a'. What this project is wanting is a random index from 'a' on the return statement.

You have gotten a random index with random.randint(0, len(a)) - 1. This is the correct way to get a random index. Good work. Now you just need to store the random index in a variable and use it in the return statement to return a random index from 'a'.

I will let you put the pieces together to make your function work. If you have any issues or questions about anything let me know.

I'm also lost on this one. I've tried several iterations and can't figure out exactly what I'm missing.

import random

word = ("Potatoes")

def random_item(word):
    rand = random.randint(0, len(word)) - 1
        return rand
import random

number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

You are very close Paul. The first code snippet where you return "rand" you are just returning the random number so that one does not work. The second code snippet you have nearly solved this challenge. There are two reasons why this snippet does not pass the challenge.

The first reason is that your return statement is indented over. Python forces correct indentation for readability and ease of use. It may cause some confusion now, but later on you will appreciate this. Just line up your return statement with "rand" and you are good. The second is that you are trying to get the index incorrectly. Many functions are preformed in the .lower() or .upper() style. Getting the index is not one of those. To get an index use "return number[rand]". This is just one of those things you will remember over time.