Q: How can I turn a user input into a global function? (Python) I'm trying to create my own text game, as instructed in Ex 45 of Learning Python the Hard Way, but I'm a bit stuck. I have my own inventory system set up and I need to figure out how to display a list of the inventory any time the user types "inventory" (I also want to do similar things for "help")... I know that I could add an if-statement after every raw_input for it, but I wanted to see if there was some global function or code that I could use to make this a lot easier on me.
I've searched and searched but I have no idea how to phrase the question or what I could possibly do to fix this. Here's my inventory set-up, with how I'm using to call it:
class Item(object):
 def __init__(self, name, quantity=1):
    self.name = name
    self.raw = name.strip().lower()
    self.quantity = quantity

 def recalc(self):
    self.netValue = self.quantity * self.value

class Container(object):
 def __init__(self, name):
    self.name = name
    self.inside = {}

 def __iter__(self):
    return iter(self.inside.items())

 def __len__(self):
    return len(self.inside)

 def __contains__(self, item):
    return item.raw in self.inside

 def __getitem__(self, item):
    return self.inside[item.raw]

 def __setitem__(self, item, value):
    self.inside[item.raw] = value
    return self[item]

 def add(self, item, quantity=1):
    if quantity < 0:
        print "ERROR."
    if item in self:
        self[item].quantity += quantity
        self[item].recalc()
    else:
        self[item] = item           

inventory = Container("Inventory")

And here's what I'm using to call it when inventory is typed:
print "Inventory: [" + ",".join((item[1].name for item in inventory)) + "]"


A: you could use a dictionary combined with lambdas, this would be compact and fast
from __future__ import print_function

#store commands
inputLookup = {"Inventory" : lambda: print ("Inventory: [" + ",".join((item[1].name for item in inventory)) + "]"), "other text" : lambda: 3} #and so on

text = input() #replace with hover you get input
inputLookup[text]()  # call the function from dictionary at that command

the import will change all your print statements to a print() however.
For lambdas you can also do.
def f():
    print "Inventory: [" + ",".join((item[1].name for item in inventory)) + "]"

inputLookup = {"Inventory": lambda: f()}
text = input()
try:
    inputLookup[text]
except:
    print "that is not a valid input" #or something

this will allow you to use the old print statment

yes, you should never use recursion to the same function in this manner, you'll generate a large stack. Also, those print statements are in a weird place.
def func1():
    print "Uh oh."
    print "You're outside the house and it's quiet."
    print "You EXAMINE and find a ROCK."
    While True:
        userinput1 = raw_input(">>> ")
        if userinput1 == "inventory":
            print "Inventory: [" + ",".join((item[1].name for item in inventory)) + "]"
        elif userinput1 == "pick up": 
            if rock not in inventory:       # so they can't pick up infinite rocks   
                inventory.add(rock)
                print "You picked up a ROCK."
            else:
                print "you've already picked up the rock"
        elif userinput1 == "examine":
            print "Man's best friend."
        else:
            print "Let's go on about our way then."
            break

I failed to see you were just starting out. I would stay away from lambda's for the moment, as they're a bit more advanced, sorry for the confusion. Before, if you look at your if/elif/else tree, there was no way for the else statement to be called, so i've edited it a bit, i hope it functions the way you intended. Also, are inventory and rock globals? if not you would need to declare or reference them in the function. (although as a future note, staying a way from globals is generally good)

hmm...i assume you mean items like the rock and such.
Items = {'rock': Rock(), 'item2': Item2(), 'item3' : item3()} #where Item() is creating the object
Inventory.add(Items[itemname]) # adds the item

You could make Items as a global so you can access it everywhere. This is ok for now, but as you learn, you should generally stay away from globals.

A: You could create a function for each valid user input, such as inventory
Then, you could do:
input = raw_input('...')
self.method = getattr(<your class>, input)
self.method()

which would call
def inventory(self, ...):
    pass

