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

Mark Coup
Mark Coup
1,112 Points

I'm having trouble referencing the argument specified in the function I've created. Please help.

I think that I'm generating a random number correctly, just having trouble referencing the argument. How can I get the correct reference?

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

3 Answers

HI Mark

see below

# EXAMPLE
import random
# random_item("Treehouse")
def random_item("Treehouse"): # the purpose of a function is a pass a parameter rather than a static string 
    # The randomly selected number is 4.
    index_num = random.randint(0, len(Treehouse) - 1 # your missing a closing bracket
    index_num = int(index_num)
    # The return value would be "h"
    if index_num is True: # not sure what this condition is meant to be doing? because index_num is always true
        return Treehouse[index_num]


# see my code below

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

print(random_item("Treehouse"))   

# it does return h for the random index of 4 

hope this helps

Mark Coup
Mark Coup
1,112 Points

Thanks man, it did work. But I have a question. Why did you try to print(random_item("Treehouse")) at the end of your code? Mine worked with just the return block. And I'm not sure what you're referencing because you don't have Treehouse in any part of your code except that print.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

From a syntax perspective, you are using a string literal as the function parameter. It is not a mutable object, that is, it can not be changed or assigned a value.

Remove the quotation marks and use just the plain variable name Treehouse.

Considering the PEP 8 style guideline, the parameter name should not be capitalized, so use the name treehouse instead.

Mark Coup
Mark Coup
1,112 Points

Thanks Chris. So you're saying that because the Treehouse string is a function parameter, it doesn't need any quotation marks? In the example given, Treehouse is in quotations marks.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

More broadly I'm mean string objects can not be used as function parameters. Using treehouse as a parameter without quotes defines a "placeholder" variable name that will receive the object listed in the function call argument list. This placeholder variable becomes a local variable inside the function.

On the call to the function randon_item(), the argument "Treehouse" is allowed. This string will be assigned to the local parameter Treehouse (or treehouse if using proper lower-case parameter names)

i come up with this:

import random

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