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

anhedonicblonde
anhedonicblonde
3,133 Points

Random

I did the letter game (with a few errors) but seem to have missed something as this Code Challenge is a mystery to me. Could someone go over the code with me so I can understand the process? Thanks in advance. :)

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

2 Answers

Haider Ali
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Haider Ali
Python Development Techdegree Graduate 24,728 Points

Hi, I understand you are having trouble with this challenge, I will explain it to you:

import random                                  #import the random module

def random_item(iterable):                     #define the function that takes 1 iterable
  index = random.randint(0, len(iterable) - 1) #this is the index of the item that will be returned
                                               #get the index by getting a random number between 0 and the length of the list - 1
  return iterable[index]                       #return what ever is at that index in the iterable that was passed in

I think the reason that you were struggling with this challenge is because you may not have read the question correctly, make sure you pay careful attention to the question next time :)

Thanks, Haider

anhedonicblonde
anhedonicblonde
3,133 Points

thank you so very much - you are correct, I did not understand the question and did not even define a function. I think I still would not have gotten it though might have gotten close - your explanation has helped me immensely! thank you so much!! :)

Parzeval One
Parzeval One
1,920 Points

What is the "minus 1" all about?

I dont get that at all. And also for me this is a loop kind of thing, no? until you have random number and index of h, keep trying. It is so hard to think code... :-(

Here is the version of the answer that would make sense, it includes "Treehouse", index 4 and the letter "h".

How am I to know that the solution is any iterable, any index and any letter?

    import random

    iterable = "Treehouse"

    def random_item(iterable):
        index = random.randint(0, len(iterable)-1)
        if index == 4:
            return iterable[index]

    random_item(iterable)
Haider Ali
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Haider Ali
Python Development Techdegree Graduate 24,728 Points

Hi there Parzeval, the -1 is used because indexes start from number 0. For example, in a list called my_list the first items in that list would be my_list[0] not my_list[1]. This is why we minus 1 so we can get the correct index.