简体   繁体   English

布尔语句和python中的if语句的问题

[英]Problems with boolean statement and if statement in python

def test():
    boo = True
    enter = int(raw_input("Enter something: "))
    if enter > 0:
        boo = False
        print "Did it work?!"
    else:
        print "Still working..."
    while boo == True:
        test()

test()

So this bit of code seems to only work if the first part of the if statement is met first try. 因此,这段代码似乎只有在首先尝试满足if语句的第一部分时才起作用。 aka if you enter -1 then 1 it continues to loop through the function. 也就是如果您输入-1,则1会继续遍历该函数。 What am I doing wrong? 我究竟做错了什么?

boo is a local variable. boo是一个局部变量。 Every time you recursively call test() , that recursive call gets a new local variable boo that is independent of all parent functions calling it. 每次您递归调用test() ,该递归调用都会获得一个新的局部变量boo ,该变量独立于所有调用它的父函数。 As such, it is set to True at the beginning of the function. 因此,在函数开始时将其设置为True

Your calls look like this: 您的通话如下所示:

  • test() , boo = True , enter = 1, so while boo == True is true: test()boo = Trueenter = 1,因此while boo == True为true:

    • test() , boo = True , enter = -1, so while boo == True is false, returning test()boo = Trueenter = -1,因此while boo == True为false时,返回
  • at this level, boo == True is still true , so we call test() again. 在此级别上, boo == True 仍然为true ,因此我们再次调用test()

    • test() , boo = True , enter = -1, so while boo == True is false, returning test()boo = Trueenter = -1,因此while boo == True为false时,返回

etc. ad nauseum. 等等。

You want to return boo : 您想返回 boo

def test():
    boo = True
    enter = int(raw_input("Enter something: "))
    if enter > 0:
        boo = False
        print "Did it work?!"
    else:
        print "Still working..."
    while boo:
        boo = test()
    return boo

Now when test() returns, you set boo in the calling function to False . 现在,当test()返回时, 将调用函数中的 boo设置为False

Note that == True is redundant, just test for boo itself in while boo . 注意== True是多余的,只需在while boo测试boo本身。

It is really not a good idea to use recursion for user input. 使用递归进行用户输入确实不是一个好主意。 Just use a while loop: 只需使用while循环:

while True:
    enter = int(raw_input("Enter something: "))
    if enter < 0:
        print "Did it work?!"
        break
    print "Still working..."

Combining a potentially infinite while loop with a recursive function is a very dangerous game. 将可能无限的while循环与递归函数结合在一起是非常危险的游戏。 When you get to subsequent calls of test() , even if they work and exit out right away, your original call to test() will still loop forever. 当您到达随后的test()调用时,即使它们正常工作并立即退出,您对test()原始调用仍将永远循环。 boo is never changed in the original function call just because it's changed in later ones. boo永远不会在原始函数调用中更改,只是因为它在以后的函数中已更改。

Your code could be fixed this way: 您的代码可以通过以下方式修复:

def test():
    boo = True
    enter = int(raw_input("Enter something: "))
    if enter > 0:
        boo = False
        print "Did it work?!"
    else:
        print "Still working..."
    if boo == True:
        test()

But it would be even better this way: 但是这样会更好:

def test():
    enter = int(raw_input("Enter something: "))
    while enter <= 0:
        print "Still working..."
        enter = int(raw_input("Enter something: "))
    print "Did it work?!"

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

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