Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old October 21st, 2003, 02:23 PM
JazzFusion JazzFusion is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 4 JazzFusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old October 21st, 2003, 02:38 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #3  
Old October 21st, 2003, 03:14 PM
JazzFusion JazzFusion is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 4 JazzFusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #4  
Old October 21st, 2003, 03:34 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #5  
Old October 21st, 2003, 05:45 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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

Reply With Quote
  #6  
Old October 22nd, 2003, 05:35 PM
Bandung Bandung is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 3 Bandung User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #7  
Old October 23rd, 2003, 02:30 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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 11:33 AM.

Reply With Quote
  #8  
Old October 23rd, 2003, 07:56 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #9  
Old October 23rd, 2003, 11:23 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #10  
Old October 26th, 2003, 07:10 PM
JazzFusion JazzFusion is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 4 JazzFusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > GOTO not in Python


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway