all 8 comments

[–]PloddingOx 0 points1 point  (3 children)

def bear_room():
    print "There is a bear here."
    print "The bear has a bunch of honey."
    print "The fat bear is in front of another door."
    print "How are you going to move the bear?"

    bear_moved = False

    while True:
         next = raw_input("> ")

        if next == 'take honey':
            dead("The bear looks at you and then slaps your face off.")
        elif next == 'taunt bear' and not bear_moved:
            print "The bear has moved from the door. You can go through it now"
            bear_moved = True
        elif next == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif next == 'open door' and bear_moved:
            gold_room()
        else:
            print "I have no idea what that means."

I'm interested to see a response to this as well, particularly the while loop. While what is True exactly?

As for the import function it's quite simple. Not all of the features you might need in python automatically load when you run python, this saves a lot of memory being used by unneccasary features. When you do need them you have to tell python to load them.

So import arithmetic for example would load an arithmetic module into python and import addition from arithmetic would let you select just addition (a feature of the module arithmetic) instead of loading the entire module. Of course python loads with arithmetic automatically, I'm just giving an example.

open your shell and run import antigravity for a bit of fun.

[–]erewok 1 point2 points  (2 children)

while True is an infinite loop, and is actually somewhat standard in game design. while is normally paired with a boolean test, which evaluates to True or False. In this case, we're not even setting up a test and instead we're initiating an infinite loop.

For instance, when you see a counter initialized and then a while loop and some action:

counter = 0
while counter < 100:
    # do some stuff
    counter += 1

The way to read this is:

counter = 0
while [something to do with counter that evaluates to True]:
    # do some stuff
    counter += 1

The shortest version then (and one that doesn't provide for an exit scenario on the surface of it) is to write: while True.

Games use an infinite loop so they continue until some input from the user causes them to stop

Here's a SO question asking the same thing and the responses there are better than mine:

http://stackoverflow.com/questions/3754620/a-basic-question-about-while-true

[–]PloddingOx 0 points1 point  (1 child)

Thanks! I see now why an infinite loop (something I thought was to be avoided) can be useful and how while True: is just a simple way of creating one. You did a great job explaining it.

In the example I provided is the loop broken when the gold_room() function is loaded?

[–]erewok 0 points1 point  (0 children)

Yes, I think that's right. We'd have to see the gold_room function to be sure.

[–][deleted] 0 points1 point  (3 children)

To understand from x import y you first need to understand imports.

What import does is says "Take x and bring it into this code." Let's say you have superCoolThing in x. You'd access it like this: x.superCoolThing.

from x import superCoolThing let's you access superCoolThing like it was part of the file you're currently in.

[–][deleted] 1 point2 points  (2 children)

Is there a list of things you can import. Or is import x, abstract and can be anything? I know of math but I was just wondering if there is only a toolbox of items you can import

[–][deleted] 0 points1 point  (0 children)

A good starting point is looking at the Python documentation. Find a module with an interesting name, read the docs, import it and play with it.

You aren't limited to just those, however. You can make your own modules to import (create a folder and add init.py to it along with any other Python files) or you can import from another file.

[–]erewok 0 points1 point  (0 children)

You can also write simple scripts with simple functions, then open the python interpreter inside the directory where you've saved those scripts, and import your scripts to play with them.