#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0

    GOTO not in Python


    I have just switched from Visual Basic 6.0 to Python and ran into my first gotcha. I am rewriting a program I wrote a few years back in Visual Basic. There is a routine I jump to many times (from within a number of subroutines) and don't want to return, hence not using CALL.

    Since there isn't a GOTO statement in Python, how can I accomplish this task? I know all the arguments of why there isn't a GOTO statement in Python and, although they may very well be true, that doesn't help me any.

    Any suggestions (besides rewriting the whole program from scratch) would be greatly appreciated. What I need to do is jump to a subroutine and not return.

    Thanks, Jim
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    It would be easier to help you if you described why you don't want to return, and from which subroutine you don't want to return, etc.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0
    The program I wrote is a numerology program. The user inputs their first, middle and last name ... their nickname and any new lastname (for married women). Then they input their date of birth.

    During the calculation routines, if any of the above information is NULL, I jump out of the routine which sends a message to the user that all the information wasn't filled in completely. I don't want to return because the routine would continue and do the calculations on NULL values.

    Thanks for the help,
    Jim
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Mmmm, i don't know VB but that seems like a very anoying way to do things.. have you tried using an if-elif-else statment to check if any of the inpute'd date equals None (pythons NULL), and put your function call inside the else. This way you don't have to worry about jumping in and out of functions

    And a lil pseudocode..

    Code:
    if var1 == None:
        ...
        if var1 is None.
        Also, put error/warning message here.    
        ...
    elif var2 == None:
        ...
        if var1 isnt None but var2 is None.
        Also, put error/warning message here.
        ...
    else:
        ...
        if all the vars above are not None.
        call function here.
        ...
    Hope this helps with your problem,

    Have fun,
    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    Also, if you want to stop the function from returning, try one of these methods:

    1 - just use sys.exit to quit the program

    2 - use "break" to quit the loop in which the values are being processed
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    3
    Rep Power
    0
    I programme a lot in VB and have had to convert some pretty old routines myself. The two methods that I have used to circumvent the GOTO are the two just mentioned.

    if .. then ... else works fine for most things and the other method,
    using the break or exit function is exactly equivalent to VB's way of bailing out of a subroutine jsut before errorhandling.

    Anyone who has written error handling routines within VB, invariably has to use the following procedures; eg


    on error Goto ERRORHANDLER

    stuff
    foo,bar
    stuff
    ...
    ...

    exit sub
    ERRORHANDLER:
    stuff to handle error
    end sub

    Note, one has to use the "exit sub" call in order to prevent the execution of the code after ERRORHANDLER, for a normal process without errors. I'm new to Python myself so its gratifying to see how old tricks learned elsewhere can be reused within Python.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Python's error handling is very cool! definatly the nicest i've seen so far.. although i do like the 'or die' in perl for one liners!

    Code:
    try:
        ...
        some problem code
        ...
    except [optional error type]:
        ...
        run this block on error
        ...
    else:
        ...
        if not one of optional error codes above
        ...
    Code:
    try:
        ...
        some problem code
        ...
    final:
        ...
        do this if the try block has an error.
        ...
    kinda if-else style error handleing (similar to java?), but I've never had to use the try-final block for anything yet

    Mark.
    Last edited by netytan; October 23rd, 2003 at 12:33 PM.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    As a matter of fact, in the above code, you cannot use the 'finally' clause. In Python, you write either try-except-else blocks, or try-finally blocks. There's no such beast as a try-except-finally block in Python.
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Oopsy my bad, remembered that one wrong! Pffft It's always easier to remember things when you've used them , and i've never actually used final so.. i'll change it now

    Mark.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0
    Thanks for the great ideas! I stil haven't figured out the best way to implement this but am using your ideas. I'll let you know when I get it down.

    Thanks, Jim

IMN logo majestic logo threadwatch logo seochat tools logo