GOTO-type function in Python?

Hey! It aint that hard!
Post Reply
AndrewB
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 2385
Joined: 12/09/01, 12:00 am
Location: USA
Contact:

Post by AndrewB » 10/16/02, 11:21 am

Does Python have any means of going from one set of statements to another based on things other than for, while, if, etc. such as the GOTO in BASIC? I haven't run accross one, but if there is something like that, it would be very helpful to me.
thanks,
adb
Keep Your Assets & ID Private! Can you afford not to?

brantgurga
Senior Member (Entitled To Root Beer)
Senior Member (Entitled To Root Beer)
Posts: 529
Joined: 01/08/02, 12:00 am
Location: Indianapolis, IN
Contact:

Post by brantgurga » 10/16/02, 1:46 pm

I don't know Python, but in general isn't GOTO and its variants considered bad programming practice. It is usually better, easier, and more logical to call a subroutine.
Brant Gurganus

Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 10/16/02, 2:06 pm

I'm with Brant on this one, what is the situation where you want to use it and why GOTO?
I answer rhetorical questions for my own enjoyment.

AndrewB
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 2385
Joined: 12/09/01, 12:00 am
Location: USA
Contact:

Post by AndrewB » 10/16/02, 2:14 pm

i figured out how to do it w/ while and if tests but now ive got a problem... how do I terminate an app? After my program finishes its last task, it goes back to its first routine. my code is as follows:

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class=postbody>Code:</font><HR width=100% color=#333333 SIZE=1></TD></TR><TR><TD><FONT class=code><PRE>#File I/O, while loops, etc.

#initial variables
a = 0
#Password Prompt
print "Welcome to the program! "
print "Please input your name and password!"

#While w/ nested if to prevent continued execution until the correct
#password is put in. If 3 incorrect passwords are tried, the program exits.
usrname= raw_input("Username: ")
import string
fusrname = string.upper(usrname[0])+usrname[1:]
while a < 3:
password = raw_input("Password: ")
a = a + 1
#print a
#File I/O stuff...
if password <IMG SRC="/phpBB/images/smiles/icon_samurai.gif"> "adb":
iostring = "Welcome to the jungle! nWe have fun & games!n"
nextline = raw_input("Input the next verse: ")
final = iostring+nextline
output = open('C:pytest.txt', 'w')
output.write(final)
output.close()
print '*'*50

input = open('C:pytest.txt', 'r')
inputstring = input.read()
print inputstring
#password = raw_input("Password: ")
elif a >= 3:
print "Sorry,",fusrname,"but you've input the wrong password",a,"times!"
</PRE></FONT></TD></TR><TR><TD><HR width=100% color=#333333 SIZE=1></TD></TR></TABLE><!-- BBCode End -->
Keep Your Assets & ID Private! Can you afford not to?

AndrewB
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 2385
Joined: 12/09/01, 12:00 am
Location: USA
Contact:

Post by AndrewB » 10/16/02, 2:42 pm

Well, I still dont know how (or if its possible) to initiate an explicit termination, but my final code here doesn't have the problem of returning to the password prompt after outputting the finished file.

Code as follows:

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class=postbody>Code:</font><HR width=100% color=#333333 SIZE=1></TD></TR><TR><TD><FONT class=code><PRE>#File I/O, while loops, etc.

#initial variables
a = 0
#Password Prompt
print "Welcome to the program! "
print "Please input your name and password!"

#While w/ nested if to prevent continued execution until the correct
#password is put in. If 3 incorrect passwords are tried, the program exits.
usrname= raw_input("Username: ")
import string
fusrname = string.upper(usrname[0])+usrname[1:]
while a < 3:
password = raw_input("Password: ")
a = a + 1
#print a
#File I/O stuff...
if password <IMG SRC="/phpBB/images/smiles/icon_samurai.gif"> "adb":
iostring = "Welcome to the jungle! nWe have fun & games!n"
nextline = raw_input("Input the next verse: ")
final = iostring+nextline
output = open('C:pytest.txt', 'w')
output.write(final)
output.close()
print '*'*50

input = open('C:pytest.txt', 'r')
inputstring = input.read()
print inputstring
a = a + 3
#password = raw_input("Password: ")
elif a >= 3:
print "Sorry,",fusrname,"but you've input the wrong password",a,"times!"

</PRE></FONT></TD></TR><TR><TD><HR width=100% color=#333333 SIZE=1></TD></TR></TABLE><!-- BBCode End -->

All i had to do was overload the first while statement by adding 3 to the variable "a" making the outcome at least 4. At the end, i changed the elif statement to "a >=3".
Keep Your Assets & ID Private! Can you afford not to?

Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 10/16/02, 3:04 pm

I get ya, you just need to shuffel the code around a little and add an and operator. The code below is just the basic layout. It works by checking both the reiteration number is less than 3 and the password is incorrect before running the "while" statement. Then after it has either run three times or the password is correct it exits the while loop and then goes onto the "if/else" statement which checks if the password is correct or not.

My brain isn't primed for coding this early in the morning but I'm pretty sure I got that right and it's at least close to as efficent as possible. You will ofcourse need to change the syntax so that it's compatible with Python, but from the coding you have done so far, I'm guessing all you need is the layout and you can figure out the rest.

while(a<3 && password != "adb"){
run password input
a = a + 1
}

if(password != "adb"){
you entered the correct password
}
else
{
the password was incorrect 3 times
}
I answer rhetorical questions for my own enjoyment.

AndrewB
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 2385
Joined: 12/09/01, 12:00 am
Location: USA
Contact:

Post by AndrewB » 10/16/02, 4:16 pm

Yeah I did it like this (also checks username now too):

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class=postbody>Code:</font><HR width=100% color=#333333 SIZE=1></TD></TR><TR><TD><FONT class=code><PRE>
if password <IMG SRC="/phpBB/images/smiles/icon_samurai.gif"> "adb" and usrname <IMG SRC="/phpBB/images/smiles/icon_samurai.gif"> "Andrew":
</PRE></FONT></TD></TR><TR><TD><HR width=100% color=#333333 SIZE=1></TD></TR></TABLE><!-- BBCode End -->

and added near the end (new 1st elif):

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class=postbody>Code:</font><HR width=100% color=#333333 SIZE=1></TD></TR><TR><TD><FONT class=code><PRE>
elif usrname != "Andrew":
usrname = raw_input("Whats your username? ")
</PRE></FONT></TD></TR><TR><TD><HR width=100% color=#333333 SIZE=1></TD></TR></TABLE><!-- BBCode End -->
Keep Your Assets & ID Private! Can you afford not to?

SOD
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5284
Joined: 12/06/01, 12:00 am
Location: here and there

Post by SOD » 10/16/02, 7:04 pm

looks like scriptage to me with a little different syntax. Andrew have you seen this?
http://wxpython.org/what.php

AndrewB
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 2385
Joined: 12/09/01, 12:00 am
Location: USA
Contact:

Post by AndrewB » 10/17/02, 9:08 am

yes Python is a scripting language, technically, albiet a *very* powerful one. Does this toolkit integrate into the standard Python IDE, or is it separate? It doesnt say for certain so I'm assuming its seperate...

Python has its own integrated GUI modules as well, btw.
Keep Your Assets & ID Private! Can you afford not to?

Post Reply