简体   繁体   English

子手计划:循环验证

[英]Hangman Program: Loop Validation

I am writing a hangman game program in python and have run into a problem concerning validating loops. 我正在用python编写子手游戏程序,并且遇到了有关验证循环的问题。 Here is an example of the type of problem: 这是问题类型的示例:

    def loopGet():
        condition = True
        while condition == True:
            userInput = raw_input("Enter a string: ")
            # assigns boolean value to condition
            condition = ifWon()
            # returns condition
            return condition
            #assigns boolean value
            condition = ifLost()
            #return condition
            return condition

Pretending that ifWon() and ifLost() are already created, this is an example of what's going and what I am trying to figure out. 假装已经创建了ifWon()和ifLost(),这是正在发生的事情以及我要弄清楚的事情的一个示例。 In my program, even if both return True, the loop ends. 在我的程序中,即使两个都返回True,循环也会结束。 If one returns True and the other False, it still ends. 如果一个返回True,另一个返回False,则仍然结束。 I am under the impression that if "condition" returns True, the loop should keep running; 我的印象是,如果“ condition”返回True,则循环应继续运行; yes? 是?

So basically you want to loop until ifWon() or ifLost() are True. 因此,基本上,您要循环播放直到ifWon()或ifLost()为True为止。

In the original code, when you 'return condition' outside an "if", you'll just leave the loop right away. 在原始代码中,当您在“ if”之外“返回条件”时,您将立即离开循环。 In fact, you'll never reach ifLost(), because you'll just bail out in the line before that. 实际上,您永远都不会到达ifLost(),因为您将在那之前退出困境。

I don't think you need "condition" at all... just loop until you get to a True, and return then. 我认为您根本不需要“条件”……只是循环直到您到达True,然后再返回。 Note how the returns are inside an if, so you only return if True. 请注意,返回值是如何包含在if中的,因此仅返回true。

  def loopGet():
    while True:
        userInput = raw_input("Enter a string: ")
        # do something with input
        if ifWon():
            return 'Won'
        if ifLost()
            return 'Lost'

Some people might object to the nested return, but that's a pretty straightforward way to do it. 有些人可能会反对嵌套收益,但这是一种非常简单的方法。

The loop "ends" because the method's execution stops at the return statement. 循环“结束”,因为该方法的执行在return语句处停止。 Since your return statement is inside your while loop, it appears that you condition is not working. 由于您的return语句在while循环内,因此您的条件似乎不起作用。

Once you move the return statement outside your loop, you'll get your expected results. 一旦将return语句移出循环,您将获得预期的结果。


def game():

    isWinner = False
    while isWinner == False:
       # as the user for a guess
       # check the guess
       isWinner = isWon()
    return "You won!"

You set a variable to the condition that you want to change. 您将变量设置为要更改的条件。 So isWinner = False , then while isWinner == False: do your loop. 所以isWinner = False ,然后while isWinner == False:做你的循环。 If isWon() returns True or False , then in your while isWinner = isWon() . 如果isWon()返回TrueFalse ,则在您的isWinner = isWon() You don't need isLost() , as we want the loop to exit when the user has won. 您不需要isLost() ,因为我们希望在用户获胜时退出循环。

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

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