'How to jump to a specific line in py3 [duplicate]

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!



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alex