我需要修正什么才能使这个代码返回“True”?

2024-05-29 10:32:24 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是我正在处理的项目的一些代码:

# Make sure that the_flying_circus() returns True
def the_flying_circus():

    if (3 < 4) and (-10 > -20):# Start coding here!
        print "Hey now!"# Don't forget to indent
        # the code inside this block!
    elif (-4 != -4):
        print "Egad!"# Keep going here.
    else:
         return True # You'll want to add the else statement, too!

我不知道为什么这不满足返回True的条件。有什么想法吗?在


Tags: theto项目代码truemakethathere
3条回答

此条件始终是TRUE

if (3 < 4) and (-10 > -20):# Start coding here!
    print "Hey now!"# Don't forget to indent
    # the code inside this block!

因此脚本将始终打印:Hey now!。 不可能,代码将在任何一种情况下结束。在

^{pr2}$

这些条件完全没有必要。您唯一能做的就是更改第一个条件(例如,使用一些变量,它将采用不同的值,这样就不会每次都是TRUE

(3 < 4) and (-10 > -20)

好吧,3小于4。并且-10大于-20。所以这个表达式总是正确的。您的函数不执行return语句,因此返回None。您的函数可以这样重写:

^{pr2}$
# Make sure that the_flying_circus() returns True def the_flying_circus():

    if (3 < 4) and (-10 > -20):# Start coding here!
        print "Hey now!"# Don't forget to indent
        # the code inside this block!
    elif (-4 != -4):
        print "Egad!"# Keep going here.
    return True # You'll want to add the else statement, too!

这将返回True。否则,你总是会听到“嘿,现在!”因为您的if条件总是True。在

如果您想在满足条件时返回True,那么代码应该是

^{pr2}$

相关问题 更多 >

    热门问题