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

help_with_code

help_with_code. thanks in advance

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
random.randint(0,len(number)-1)
number[]
def random_item():
     while True:
        number=input()
        print(number[random.int]) 

4 Answers

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

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

Treehouse has 9 letters. So we get a random int between 0 and 9-1. This int is used as index :) Like in the example h has the index of 4 in "Treehouse", because the index starts by 0. This is why we have to "len(iterable)-1". This would not be necessary if indexes would start with 1 :/

thanks Tobias but how do we then put in values into CODE?

By calling the function...but the challange will do that itself to check your code. If you want to check it just do:

// just fill the parameter...
print(random_item("Tobias"))

You are not supposed to input the number since it is a random number generated by the computer

thanks again :)