1
while True:
    user_input_1 = input("")

    if user_input_1 == 'yes':
        print('Cont..')
        # Do something

        if checkcondition_1 == True:
            # Do something

            user_input_2 = input("")
            if user_input_2 == yes:
                # Do something
            if user_input_2 == no:
                # Do something

        else:
            print("Going back")
            continue

    if user_input_1 == 'no':

        user_input_3 = input("")

        if user_input_3 == 'yes':
            break
        if user_input_3 == 'no':
            # I need help to go back to "User_Input_1 == 'yes' 
            Go all the way back to "user_input_1" =='yes' condition.
            Is it possible to go back like that?

As I have mentioned comments above, after user_input_3, if that condition equals to "no", I want to go all the way back up to user_input_1 == 'yes'. When I say continue after that statement, it only goes back to user_input_1 == 'no'. How can I go back up one more in while statement? Any suggestion would be great.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user9431057
  • 1,203
  • 1
  • 14
  • 28
  • did you try adding "pass" – venkata krishnan Jun 25 '19 at 01:58
  • 1
    When *it loops*, it will go to the very first statement after the `while` statement. If you don't want `user_input_1 = input("")` to repeat move it to just before the `while` statement. - It isn't real clear what you are trying to achieve. – wwii Jun 25 '19 at 02:12
  • @wwii In `user_input_3 == no` I am trying to really make sure user does not miss anything before they do final submit. In case, if they had forgotten and go back and do something, I wanted them to go all the way back. My scenario is some data manipulation, but explaining them can be a long process, so posted the psudeo code I am following. Hope this helps. – user9431057 Jun 25 '19 at 02:34
  • @venkatkrishnan I tried and it did not work :( – user9431057 Jun 25 '19 at 02:34
  • adding this as an answer as I couldnt put the exact code block in comment, let me know if it doesn't work. – venkata krishnan Jun 25 '19 at 03:31
  • We still don't quite understand what you mean by "all the way back". Do you want to repeat `user_input_1 = input("")`, or do you want to act as if `user_input_1 == 'yes'` is satisfied? Notice that right now the value of `user_input_1` is actually `'no'`, so if you just skip the `input`, you probably don't get what you want. – Imperishable Night Jun 25 '19 at 03:51
  • 3
    Please clarify how your code is not working as expected. In the code shown shown, it *will* go all the way back to ``user_input_1 = input("")`` and proceed according to the new input. – MisterMiyagi Jun 25 '19 at 03:51
  • @MisterMiyagi Yeah, it goes up until `user_input_1 = input("") ` but I want it to go after `if user_input_1 == 'yes':` as shown above. Because as #wwii suggested when I put `user_input_1 = input("")` outside, and have this code, it only goes until `if user_input_1 == 'no':`. Hope this helps. – user9431057 Jun 25 '19 at 13:44
  • 1
    You could create different functions for obtaining user input for differing circumstances then call the appropriate function as needed within the loop - don't forget to return a value from the function(s). Maybe Related: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). There is no `goto` so you cannot make the loop start anywhere but the first statement after the `while` statement. – wwii Jun 25 '19 at 15:01
  • @wwii I googled as much as I can and seems like we can't point it to a certain step of the while loop, unless we have functions and may be some local copies of data inside the loop based on the need. Thank you for the link above. – user9431057 Jun 25 '19 at 18:00

1 Answers1

0

You can do the process you want but not with while loop but can be done with function. I have rewritten your code to get your desired result. Try it.

def user(user_input_1='yes'):
    if user_input_1 == 'yes':
        print('Cont..')
        # Do something
        if checkcondition_1 == True:
            # Do something
            user_input_2 = input("input_2")
            if user_input_2 == 'yes':
                pass
                # Do something
            if user_input_2 == 'no':
                pass
                # Do something

        else:
            print("Going back")
            user_input_1 = input("input_1")  # These two lines are equavalent to your continue.
            user(user_input_1)


    if user_input_1 == 'no':

        user_input_3 = input("input_3")

        if user_input_3 == 'yes':
            return None   # this is equivalent to break.
        if user_input_3 == 'no':
            # I need help to go back to "User_Input_1 == 'yes' 
            #Go all the way back to "user_input_1" =='yes' condition.
            #Is it possible to go back like that?

            user()  # Calling user() function with default value, which is user_input_1 = 'yes'.

user_input_1 = input("input_1")
checkcondition_1 = True         
user(user_input_1)

I hope it may help you.

Rahul charan
  • 765
  • 7
  • 15