Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

goto

Status
Not open for further replies.

3li773

Programmer
Jan 23, 2003
20
0
0
CA
hi

i've got a question about goto's i just started to use python and i've only used basic before this

i know in basic you can reuse commands by useing branch labels like this

[hi]
print "hello"
input ">"; h
if h = 3 then goto [hi]
if h <> 3 then goto [bye]

[bye]
print &quot;you guessed wrong&quot;
goto [hi]

like that is there any way to do that in python
 
Hello !

GOTOs are considered harmfull. You shouldn't use them.
In 99,9999% of cases, they are not usefull.
You should consider using other control structures.

Ok, rethink of your algorythm: your program asks for a number forever. When this number is not 3, you display &quot;you guessed wrong&quot;.

I would write it this way:

[tt]
while True:
print &quot;hello&quot;
h = int( raw_input('>') )
if h != 3:
print &quot;you guessed wrong&quot;
[/tt]

And you would stop the program with CTRL+BREAK.
 
ok but what if i had to do this

print &quot;where to go&quot;
print &quot;store&quot;
print &quot;house&quot;
place =raw_input(&quot;>&quot;)

if place == &quot;house&quot;:
print &quot;your at your house&quot;
goback = raw_input(&quot;>&quot;)

if place == &quot;store&quot;:
print &quot;what to buy&quot;
print &quot;sword&quot;
print &quot;sheild&quot;
buy =raw_input(&quot;>&quot;)

if buy == &quot;sword&quot;:
print &quot;you bought a sword&quot;
goback = raw_input(&quot;>&quot;)

if buy == &quot;sheild&quot;:
print &quot;you bought a sheild&quot;
goback = raw_input(&quot;>&quot;)

how would i make the goback variable input go back to the part that says where to go?????
 
I agree with sebsauvage... you need to learn something called &quot;structured programming&quot;, which was invented in the 1960s. His links should be helpful.

To get you started, here's how I would write your second example in Python:

# This is the main loop. It runs forever
while True:
print &quot;where to go&quot;
print &quot;store&quot;
print &quot;house&quot;
place = raw_input(&quot;>&quot;)
if place == &quot;house&quot;:
print &quot;your at your house&quot;
elif place == &quot;store&quot;:
print &quot;what to buy&quot;
print &quot;sword&quot;
print &quot;sheild&quot;
buy = raw_input(&quot;>&quot;)
if buy == &quot;sword&quot;:
print &quot;you bought a sword&quot;
elif buy == &quot;shield&quot;:
print &quot;you bought a sheild&quot;
else:
print &quot;I don't recognize what you typed.&quot;
else:
print &quot;I don't recognize what you typed.&quot;


Notice the use of indentation... this is a common feature of ALL structured programming languages (although some use {} characters or BEGIN/END keywords in addition to the indentation).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top