Python Python Basics (2015) Letter Game App Random Item

Logan Valdez
Logan Valdez
Pro 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.
# 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

Zachary Jackson
Zachary Jackson
Treehouse Project Reviewer

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)
Zachary Jackson
Zachary Jackson
Treehouse Project Reviewer

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.