Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Letter Game App Random Item

Whats the basic thing im missing about quotations being used? The example iterable uses them.. so how do mess up?

If i simply remove the " " from Treehouse, this should run. But with the quotations it fails.

I think im missing a basic understanding of what the difference is between an iterable and something that i put in there that doesnt work.

A lamens explanation would be appreciated!

This code fails..

import random

def random_item("Treehouse"):
  rnum = random.randint(0, int(len("Treehouse") - 1))
  return "Treehouse"[rnum]

This code works

import random

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

def random_item("Treehouse"):
  rnum = random.randint(0, int(len("Treehouse") - 1))
  return "Treehouse"[rnum]

1 Answer

Steven Parker
Steven Parker
215,939 Points

When you define a function, you use variable names to represent the incoming arguments. These become local variables inside the function.

:point_right: Quotes identify literal strings, they are not allowed as parts of variable names.

Python variable names:

  • Must begin with a letter (a - z, A - B) or underscore (_)
  • can be composed of the above, plus numbers
  • are case sensitive
  • cannot be any of the reserved words of the language

Literal strings can be passed to a function, as you saw in the example. But they cannot be part of the definition.