How to make python "goto" a previous line to get more input?

17,435

Goto statements are typically used in very low level languages like assembly or basic. In higher level languages like python, they are abstracted out so they don't exist. The way you would want to do this is by using a loop(which is the abstraction of a goto statement). This can be achieved with the following code.

valid_input = False
while not valid_input:
    print ("You wake up.")
    print ("You do what?")
    seg1 = input()
    if seg1 == ("Stand") or seg1 == ("stand") or seg1 == ("stand up") or seg1 == ("Stand up") or seg1 == ("Stand Up"):
       print ("You get up")
       print ("You look around you... your in a dark room. A door hangs slightly ajar infront of you.")
       print ("You do what?")
       valid_input = True
   else:
       print ("I dont understand")
Share:
17,435
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    So i understand goto is a very bad form of coding, however i need a program to go back to a previous line when the input is incorrect in console.

    print ("You wake up.")
    print ("You do what?")
    seg1 = input()
    if seg1 == ("Stand") or seg1 == ("stand") or seg1 == ("stand up") or seg1 == ("Stand up") or seg1 == ("Stand Up"):
        print ("You get up")
        print ("You look around you... your in a dark room. A door hangs slightly ajar infront of you.")
        print ("You do what?")
    else:
        print ("I dont understand")
    

    After the else statement has run i want it to repeat line 2 and continue with the program from there... how can i do this?