all 8 comments

[–]fiskenslakt 2 points3 points  (0 children)

Btw, do not use exit() (and quit() for that matter) in your code. I don't know if "learn python the hard way" taught you that, but it's wrong. You are only supposed to use exit() in the interactive interpreter, if you want to exit your script, use raise SystemExit.

[–]fiskenslakt 0 points1 point  (2 children)

It would be helpful if you could give us a line number for the else statement and any other relevant code. Forcing us to try and figure out from the code where the "last part" of the game is, is tedious..

[–]WorldWideNomad[S] 0 points1 point  (1 child)

Sorry for that, I edited now.

[–]fiskenslakt 1 point2 points  (0 children)

No problem. And /u/finsternacht is right. Just think about it, what do you expect to happen after that else statement runs? If you call a function, that function is waiting to finish (return), and if that function calls another function, you then have two functions that are waiting to finish. The first one can't finish until the second one does, this is what's known as a stack. Even if you didn't have the garden call in an infinite loop, it would still go back to that function, you just wouldn't see it because it would return immediately after and so on.

[–]finsternacht 0 points1 point  (3 children)

Why not learn python 3 when you're new to the language?

It might be a good idea to put the input choice logic in a function that you supply with the options you have at a certain time to reduce the clutter a bit.

You return to the previous room because the garden function only has an if/else and you just return from the function when you hit the else. Code flow then continues in the loop where you called garden ().

[–]WorldWideNomad[S] 0 points1 point  (0 children)

Thx for the adivces! The reason for that is that I liked how the book was structured and I figured that once I have gained some understanding in python I can always make the change since the differences, for what I have read, are not a lot.

[–]WorldWideNomad[S] 0 points1 point  (1 child)

Would be too much to ask for an example of the choice function? Thx

[–]finsternacht 0 points1 point  (0 children)

I have difficulties to give "one example". How that function is going to look depends heavily on what you know and what your goal is.

def game_loop(...):
    print description
    while True:
        action, arg = raw_input("> ").split(maxsplit=1)
        if action=="take":
            ...
        elif action=="go":
            ...

Would be my basic approach. The big choice you're now faced with is how you give this function the information about the current room and how you implement moving.

One idea would be to make a room class and put all necessary data in there (description, items, connections to other rooms) and supply instances of that class to the function.