Is there something similar to GOTO in python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • matt murray
    New Member
    • Nov 2010
    • 4

    Is there something similar to GOTO in python?

    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


    Code:
    #Doors test
    
    def main():
    
        endprogram = 'no'
    
        while endprogram == "no"
    
            health = 100
            loot = 0
            key1='no'
            key2='no'
            key3='no'
    
            print "Welcome to Three Keys"
            print "++++++++++++++++"
            print "All choices are to be typed in lowercase"
            print "Find the three keys to access the treasure"
            print "There are many dangers and secrets to find"
            print "Be mindful of hints and keep your wits about you"
    
    def firstroom():
    
        print "Before you you see three doors
    
    def armory():
    def library():
    def baths():
    def keyroom1():
    def keyroom2():
    def keyroom3():
    def dinning():
    def spiral():
    def dungeon():
    def throneroom():
    Last edited by bvdet; Nov 30 '10, 01:49 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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.

    Comment

    • matt murray
      New Member
      • Nov 2010
      • 4

      #3
      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?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        A simple example:
        Code:
        if condition == "X":
            start()
        elif condition == "Y":
            continue_to_next_level()

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          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.

          Comment

          • Sean Pedersen
            New Member
            • Dec 2010
            • 30

            #6
            Goto, break, and continue are bad coding style to many, because they don't follow natural code flow.

            Comment

            Working...