for-loopif-statementreturnexitskip-lists

How to return to beginning of program from inside of if statement?


I'm practicing some basic coding, I'm running a simple math program running in the terminal on Visual Studio Code.

How do I create an option to return to the beginning of the program, or exit the program after getting caught in an if statement?

Example:

#beginning of program

user_input=input('Please select "this" or "that": ')
findings=user_input

If findings == this:
        print(this)
        # How can I redirect back to first user input question, instead 
        # of just ending here? 
if findings == that:
        print (that)
        # Again, How do I redirect back to first user input, instead of 
        # the program ending here?  
# Can I setup a Play_again here with options to return to user_input, 
        # or exit program? And then have all other If statements 
        # redirect here after completion? How would I do that? with 
        # another If? or with a for loop? 
#end program

Solution

  • You can try wrapping the whole program in a while loop like this:

    while(True):
        user_input=input('Please select "this" or "that": ')
        this = 'foo'
        if user_input == this:
            print(this)
            continue 
    
        if user_input == this:
            print(this)
            continue