简体   繁体   English

While 循环发生的次数超出预期(Python)

[英]While Loop occuring once more than intended (Python)

The while loops controlled by either the x variable won't stop after its relevant variable is set to False by the code contained within the if / elif statement.if / elif语句中包含的代码将其相关变量设置为False后,由x变量控制的while循环不会停止。 Instead of stopping the while loop, when the x variable is set to False it allows the loop to occur one more time before causing the loop to stop.而不是停止while循环,当x变量设置为False时,它允许循环再发生一次,然后导致循环停止。 What is causing this?这是什么原因造成的?

def GameIntro():
# Declaration of outer while loop variable
    x = True
# Outer while loop that I want to keep running until the player enters the 'yes' branch of the if statement
    while x:
# Declaration of inner while loop variable
        j = True
        playerName = input("Please enter your name:")
        print("\nWelcome " + playerName)
        print("At any time, type 'Help' to bring up the list of possible actions." + "\n")
        while j:
            beginGame = input(playerName + ", are you ready to begin? [Y] or [N]")
            beginGame = beginGame.upper()
            if beginGame == "Y" or beginGame == "YES":
# At this point I want this function to end and for it to return a True value. The     # print is purely for testing purposes.
                print("Yes Test")
                x = False
                j = False
                return True
            elif beginGame == "N" or beginGame == "NO":
# At this point, I want the inner loop to stop and for the outer loop to occur again.  # This works.
                print("No Test")
                j = False
            else:
                print("Please enter either [Y] or [N]")

GameIntro()

Here is the output I am getting.这是我得到的 output。

Please enter your name:Bob 
Welcome Bob 
At any time, type 'Help' to bring up the list of possible actions. 
Bob, are you ready to begin? [Y] or [N]
Y 
Yes Test 
Please enter your name:Bob2 
Welcome Bob2 
At any time, type 'Help' to bring up the list of possible actions. 
Bob2, are you ready to begin? [Y] or [N]
Y Yes Test 
Run game 
Process finished with exit code 0

The "Run game" comes from another function that is receiving the returned True from the yes branch of the if / elif statement. “运行游戏”来自另一个 function,它从if / elif语句的 yes 分支接收返回的True

I don't think you really need a separate variable, you can use the beginGame , for example as your sentinal value我不认为你真的需要一个单独的变量,你可以使用beginGame ,例如作为你的标记值

I would also recommend placing a break at every point within the loops rather than rely on return unless you cannot posssible break to a safe return state我还建议在循环中的每个点都放置一个break点,而不是依赖return ,除非您无法安全返回 state

def validInput(x):
    return x in {'YES', 'Y'}

def intro():
    playerName = input("Please enter your name:")
    print("\nWelcome " + playerName)
    print("At any time, type 'Help' to bring up the list of possible actions." + "\n")
    beginGame = 'N'
    while not validInput(beginGame):
        beginGame = input(playerName + ", are you ready to begin? [Y] or [N]").upper()
        if beginGame == "HELP":
            print('[Y] or [N]?')
        elif validInput(beginGame):
            print('Starting Game!')
    print(f'Getting {playerName} ready to play...')
    return (playerName, True)

playerName, ready_intro = intro()
if ready_intro:
    main_game(playerName)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM