By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

Is there something similar to GOTO in python?

P: 4
So I'm trying to write a text based adventure game and there are going to be points where you'll have to travel back to previous rooms. I wasn't really sure of the best way to go about this. Also along the way you'll collect 3 keys and various loot. any help would be great. thanks -Matt

Expand|Select|Wrap|Line Numbers
  1. #Doors test
  3. def main():
  5.     endprogram = 'no'
  7.     while endprogram == "no"
  9.         health = 100
  10.         loot = 0
  11.         key1='no'
  12.         key2='no'
  13.         key3='no'
  15.         print "Welcome to Three Keys"
  16.         print "++++++++++++++++"
  17.         print "All choices are to be typed in lowercase"
  18.         print "Find the three keys to access the treasure"
  19.         print "There are many dangers and secrets to find"
  20.         print "Be mindful of hints and keep your wits about you"
  22. def firstroom():
  24.     print "Before you you see three doors
  26. def armory():
  27. def library():
  28. def baths():
  29. def keyroom1():
  30. def keyroom2():
  31. def keyroom3():
  32. def dinning():
  33. def spiral():
  34. def dungeon():
  35. def throneroom():
Nov 29 '10 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 2.5K+
P: 2,851
There is no GOTO in Python. You can call a function based on a condition. By designing your code as objects (functions and classes), you can accomplish the same thing.
Nov 30 '10 #2

P: 4
I'm a bit of a noob when it comes to coding as of now. Is there any chance you could explain that slightly more in depth?
Nov 30 '10 #3

Expert Mod 2.5K+
P: 2,851
A simple example:
Expand|Select|Wrap|Line Numbers
  1. if condition == "X":
  2.     start()
  3. elif condition == "Y":
  4.     continue_to_next_level()
Nov 30 '10 #4

Expert 100+
P: 621
For information only, there is a goto add-on for Python. And the fact that most programmers don't even know it exists says enough about how much it gets used.
Nov 30 '10 #5

Sean Pedersen
P: 30
Goto, break, and continue are bad coding style to many, because they don't follow natural code flow.
Dec 5 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.