Welcome to the Treehouse Community
Looking to learn something new?
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]
# 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]
Steven Parker215,939 Points
When 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.