Re: Python Question
Sam, on host 24.61.194.240
Wednesday, June 19, 2002, at 17:09:41
Python Question posted by ang on Wednesday, June 19, 2002, at 14:34:06:
> My problem is backtracking to a previous 'while' loop so my character can re-visit previous locations that remain in the same state.
Heh. After telling you all about exceptions, I neglected to answer the question you were REALLY asking.
This is off the top of my head, but I suspect the way you'd want to structure your program would be something along these lines:
# Program starts here, with a new game. location = 'start' inventory = ['sack'] won_the_game = 0
while (not won_the_game): ____ print_location_description(location) ____ do_menu(location)
print "You won the game! Congratulations!"
def print_location_description(location): ____ if location == 'start': ________ print "You are at the start." ________ if not 'gold' in inventory: ____________ print "There is gold here!" ____ elif location == 'carnival': ________ print "You are at the carnival!" ________ print "What next?" ____ elif location == 'store': ________ print "You are at the store!" ____ else: ________ print "Uh oh! You found a bug, because" ________ print "you're somewhere I don't know."
def do_menu(location): ____ if location == 'start': ________ do_start_menu() ____ elif location == 'carnival': ________ do_carnival_menu() ____ elif location == 'store': ________ do_store_menu() ____ else: ________ print "Whoopsie."
def do_start_menu(): ____ print "1. Go north." ____ print "2. Go south." ____ print "3. Look at your inventory." ____ if not 'gold' in inventory: ________ print "4. Take the gold!" ____ choice = raw_input("Enter your choice: ") ____ if choice == '1': ________ print "You go north." ________ location = 'carnival' ____ elif choice == '2': ________ print "You go south." ________ location = 'store' ____ elif choice == '3': ________ print "Your inventory: " + `inventory` ____ elif choice == '4' and not 'gold' in inventory: ________ print "You take the gold." ________ inventory.append('gold') ____ else: ________ print "You picked an incorrect option!" ________ print "Try again!"
. . .
Get the idea? The while loop at the beginning keeps you in the game until you win it. When you win it, the code that handles the last step would set won_the_game to 1, and you'll break out of the while loop.
This is REALLY simplistic. It doesn't contain any variables about the state of things (for example, to remember whether a drawbridge is up or down). It's probably not efficient to have to "constant" options like "Look at your inventory" in every single menu -- a separate function for standard menu options, which is called every time through the while loop, is probably a better idea. And saving games is a whole different can of worms. But do you see how the structure of this works? You don't need any gotos, because the program flow is always where you want it. A variable keeps track of where you are, and each time through the loop, you get the location description, a menu, a chance to enter something, and a message that says what happened. Then the process repeats until you win.
|