Хотя верно - вернитесь к вершине иерархии - PullRequest
1 голос
/ 25 июня 2019
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?

Как я уже упоминал выше, после user_input_3, если это условие равно "нет", я хочу вернуться обратно к user_input_1 == 'yes'.Когда я говорю continue после этого утверждения, оно возвращается только к user_input_1 == 'no'.Как я могу вернуться еще на одну в while заявлении?Любое предложение было бы замечательно.

1 Ответ

0 голосов
/ 25 июня 2019

Вы можете выполнить нужный процесс, но не с помощью цикла while, но можно сделать с помощью function.Я переписал ваш код, чтобы получить желаемый результат.Попытайся.

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)

Надеюсь, это может вам помочь.

...