0

Sorry if this is a dump question; but I was wondering if its possible to skip parts of code in your program/loop back to a previous part (but mostly skip the next section as of right now); I'm currently working on a terminal text based adventure game and it'd be the easiest solution to the issue I'm having with it!

I've tried looking online and people kept telling me to use 'goto' but I cant figure it out

i've tried setting it as a variable

from turtle import goto


print('Hello World')
goto(testgoto)
print('Filler text')
print('Filler text')
print('Filler text')
print('Filler text')
print('Filler text')
print('Filler text')
print('Filler text')
testgoto = print("Hello Programmer")

Skipping to a specific line

from turtle import goto


print('Hello World')
goto(13)
print('Filler text')
print('Filler text')
print('Filler text')
print('Filler text')
print('Filler text')
print('Filler text')
print('Filler text')
print("Hello Programmer")

Any help would be greatly apricated thank you!

Xanthus
  • 55
  • 1
  • 9

1 Answers1

0

I think you should learn functions, here's a simple example for checking a keypress:

import keyboard

def key_pressed(letter):
    print("You pressed p")

while True:
    if keyboard.read_key() == "p":
        key_pressed("p")
        break

Goto approach is really not the best in languages that can handle functions.

alex
  • 10,900
  • 15
  • 70
  • 100
Pitto
  • 8,229
  • 3
  • 42
  • 51
  • 1
    I'll look into rewriting most of my code with functions in mind, its just that the goto approach would have been perfect for what I needed. If you don't mind me asking why is it that a lot of people seem to dislike/hate goto? – Xanthus Apr 07 '22 at 09:44
  • While it can look like a handy approach for very short code or small examples it will make future changes / updates and / or debugging your code very hard. Once you learn more about functions you will see why there is really no need to use goto. Happy hacking and come back for more questions (be sure that your question is not a duplicate like in this case :) ). – Pitto Apr 07 '22 at 09:47
  • Alrighty thank you so much for your help! I didn't think to actually check stack for the answer instead I just googled it. that's my bad – Xanthus Apr 07 '22 at 09:49