Python Forum
Is there a goto like there is in BASIC?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a goto like there is in BASIC?
#1
I did a lot of programming in MS Visual BASIC 6 way back when.  Now I'm getting up to speed in Python.  To learn Python's equivalent of If/Then/ElseIf, I've written the following code:

intLangChoice = int(input("Choose your language, 1 for French, 2 for English, 3 for German, or 4 for Spanish. "))
if intLangChoice == 1:
    print("On parle en français aujoud'hui.")
elif intLangChoice == 2:
    print("We're speaking in English today.")
elif intLangChoice == 3:
    print("Wir sprechen heute auf Deutsch.")
elif intLangChoice == 4:
    print("Hoy hablamos en español.") 
else:
    print("You didn't choose a language.")
It's a good start.  It runs.  However, I would like to make them choose a language if they put something in other than 1, 2, 3, or 4.  If they put in 5 or higher, they'll get "You didn't choose a language."  At that point, I'd like to send them back to the beginning of the code where it says, "Choose your language, 1 for French, 2 for English, 3 for German, or 4 for Spanish." 

If I mixed in the BASIC "goto" command with this code, it would be like this:

TheTop: 
intLangChoice = int(input("Choose your language, 1 for French, 2 for English, 3 for German, or 4 for Spanish. "))
if intLangChoice == 1:
 print("On parle en français aujoud'hui.")
elif intLangChoice == 2:
 print("We're speaking in English today.")
elif intLangChoice == 3:
 print("Wir sprechen heute auf Deutsch.")
elif intLangChoice == 4:
 print("Hoy hablamos en español.")
else:
 print("You didn't choose a language.")
  GoTo TheTop
I googled around, but what I found didn't run.
Reply
#2
BDFL forbids - no GOTO You just put this block into a loop - one variant is infinite loop

while True:
    if <bla-bla-bla>:
    .......
    else:
        print('You chose wrong')
        continue
    break
Unless you make a legal choice, your code will fall through to the final else, where continue will force the loop to return to beginning. On legal choice, loop will be ended by break
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Python doesn't have a goto. Goto is generally considered a bad thing to have in a language. For your code I would do a while loop:
while True:
    # your code here
    if intLangChoice in (1, 2, 3, 4):
        break
Also, with Python's dynamic typing, Hungarian notation is rather out of place.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
while True:
   choice = 0
   # go in a loop to see if the number is in the list of choices.
   while choice not in (1,2,3,4):
       try:
           choice = int(raw_input("Choose your language, 1 for French, 2 for English, 3 for German, or 4 for Spanish. "))
       except ValueError:
           print("Oops, I was expecting a number ...")

   # handle options.
   if choice == 1:
       print("On parle en francais aujoud hui.")
   elif choice == 2:
       pass # ... code ...
   elif choice == 3:
       pass # ... code ...
   elif choice == 4:
       pass # ... code ...
Reply
#5
Okay, thanks for you guys' help. Looking forward to getting up to speed in this language.
Reply
#6
Quote:Is there a goto like there is in BASIC?
My answer although late, is simple
Thank God No!
Reply
#7
(Apr-20-2017, 10:41 PM)Larz60+ Wrote: Thank God No!
Being a benevolent dictator for life does not make one a god!
Reply
#8
I know
Reply
#9
Actually, there is a goto. It was written as an April Fool's prank, but it does exist: http://entrian.com/goto/

Most languages have either removed goto, or heavily discouraged it's usage. The reasoning being that it makes your code incredibly difficult to read, even for something as simple as your example. Doing something simple, and then re-doing it sometimes, without really being very clear about when you could be re-doing it, where you could be coming from when the thing starts, how often it'll happen, or what sort of context/local variables will be available are all very difficult to think about with anything longer than just a handful of lines of code.
Reply
#10
In the VB years, they were already discouraging the use of GoTo.  It was mainly a holdover from earlier versions of BASIC.  That said, I did find it useful for certain things.  One was for when I was still in the early stages of creating a loop and wanted to avoid an endless loop and a crash.  I could set an integer to track the number of times something looped, and then set a place outside the loop named "JumpOut".  If the integer got higher than 1,000 (or some other value), "GoTo JumpOut" would be triggered and the loop would end.  Then I would test my loop until I was satisfied there were no conditions that could cause an infinite loop and then delete the GoTo code.  GoTo can certainly be abused and create spaghetti code, but I did find it useful if used cautiously. There was also opportunity for humor with code like "GoTo Hell."  

I guess in Python I'll need to find other code testing tricks.  In short, there were some things that I would allow in my code for testing purposes that I would take out once I was satisfied things were solid.  Another one was "On Error Resume Next."  That was useful to keep code running while searching for errors, but I would remove it after errors were found and fixed. 

I'll keep learning.  I'm sure I'll come across a lot of great debugging tricks in time.  Thank you so much for your useful comments.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  goto jmabrito 34 10,207 Feb-18-2021, 09:55 PM
Last Post: jmabrito
  How to make input goto a different line mxl671 2 2,456 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  goto problem Skaperen 1 2,733 Jan-27-2018, 01:11 PM
Last Post: stranac
  Go up script/menu(a goto command) foxtreat 7 8,211 Apr-24-2017, 05:58 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020