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 trialJames McCombe
Courses Plus Student 1,601 PointsWhats 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]
# 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
231,236 PointsWhen you define a function, you use variable names to represent the incoming arguments. These become local variables inside the function.
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.