如何在 Python 中的 if 语句中打破 if 语句中的多个循环 3

How to break multiple loops in an if-statement within an if-statement in Python 3

如何在 if 语句中的 if 语句中创建循环?我正忙于 如何艰难地学习 Python 3,到目前为止,我知道我可以做到以下几点:

while True:
print("choose 1st branch")
choice = input()
if choice == '1':
    print('1, now choose second branch')
    while True:
        choice = input("")
        if choice == "2":
            print("2")
            while True:
                choice = input("")
                if choice == "3":
                    print('3')#
                    
                else: #needs to ask for input for choice 3 again
                    print("try again")
        else:print("try again")#needs to ask for input for choice 2 again

我编辑了一个更简单的代码示例来说明我要完成的工作

问题是您正在重用变量 choice,并将其指定为布尔值,然后再作为输入。尝试对布尔值或输入使用另一个变量,即 proceed.

proceed = False # CREATE VARIABLE PROCEED
while proceed == False: # LOOP WHILE PROCEED IS FALSE
     print("do you reach your hand in?")
     print("\nyes \nno")
     choice = input(prompt)
     if choice == "yes":
         print("you reach into the whole and pull out a gemstone")
         print("you put the gemstone in your inventory")
         print("and go back to the room\n")
         hole = True
         gemstone = True
         proceed = True # CHANGE PROCEED, NOT CHOICE
     elif choice == "no":
         print("You go back to the center of the room")
         # Perhaps assign proceed to be True here as well?
     else: 
         print("unknown choice, try again")
occu = 'ranger'
prompt = "-->"
def room1():
print("you walk into an bare room"
door = False
gemstone = False
hole = False
monster = False
rat = False
trap = False
occu = 'ranger'
while True:
    print("you see a door infront of you")
    print("do you go left, right or foward?")
    if input(prompt) == 'left' and hole == False:
        print('\nDo you \ndodge \nor \nattack?')
        if input(prompt) == 'dodge' and occu == 'ranger':
            trap = True
            rat = True         
            print("\nhole \nroom")
            if input(prompt) == 'hole' and rat == True:
                print("you walk up to the hole, you see a faint glitter")
                while True:
                    print("do you reach your hand in?")
                    print("\nyes \nno")
                    choice = input(prompt)
                    if choice not in ['yes', 'no']:
                        print("unknown choice, try again")
                        continue # <-- Starts loop over.
                    elif choice == "yes":
                        print("you reach into the whole and pull out a gemstone")
                        print("you put the gemstone in your inventory")
                        print("and go back to the room\n")
                        hole = True
                        gemstone = True
                    else:
                        print(" you go back to the centre of the room")
                    break # <-- Breaks out of loop.

您可以将“检查用户输入是否在有效选择范围内”提取到函数中,而不是在要检查用户输入是否有效的任何地方创建循环。所述功能类似于

def get_user_choice(prompt, choices=None):
    while True:
        choice = input(prompt)
        if choices is None:
            # Return the entered choice without checking if it is
            # valid or not.
            return choice
        if choice in choices:
            return choice

        print("unknown choice, try again")

有了这个新功能,我们首先检查选项的有效性,然后在我们收到正确的输入后,我们再处理每个选项的逻辑。

occu = "ranger"
prompt = "-->"

def get_user_choice(prompt, choices=None):
    while True:
        choice = input(prompt)
        if choices is None:
            # Return the entered choice without checking if it is
            # valid or not.
            return choice
        if choice in choices:
            return choice

        print("unknown choice, try again")


def room1():
    print("you walk into an bare room")
    door = False
    gemstone = False
    hole = False
    monster = False
    rat = False
    trap = False
    occu = "ranger"
    while True:
        print("you see a door infront of you")
        print("do you go left, right or forward?")
        choice = get_user_choice(prompt, ("left", "right", "forward"))
        if choice == "left" and hole == False:
            print("\nDo you \ndodge \nor \nattack?")
            choice = get_user_choice(prompt, ("dodge", "attack"))
            if choice == "dodge" and occu == "ranger":
                trap = True
                rat = True
                print("\nhole \nroom")
                choice = get_user_choice(prompt, ("hole", "room"))

                if choice == "hole" and rat == True:
                    print("you walk up to the hole, you see a faint glitter")

                    print("do you reach your hand in?")
                    print("\nyes \nno")
                    choice = get_user_choice(prompt, ("yes", "no"))

                    if choice == "yes":
                        print("you reach into the whole and pull out a gemstone")
                        print("you put the gemstone in your inventory")
                        print("and go back to the room\n")
                        hole = True
                        gemstone = True
                        choice = True
                    elif choice == "no":
                        print(" you go back to the centre of the room")

请注意,其他输入也被修改为使用此功能来检查用户选择的选项是否有效。