我需要修复什么才能使此代码返回“True”?

时间:2014-09-29 21:06:40

标签: 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的条件。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

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

好吧,3小于4. -10大于-20。所以表达总是如此。您的函数不执行return语句,因此返回None。您的功能可以像这样重写:

def the_flying_circus():
    print "Hey now!"
    return None

答案 1 :(得分:0)

# 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,则代码应为

if (3 < 4) and (-10 > -20):# Start coding here!
            print "Hey now!"
            return True

答案 2 :(得分:0)

此条件始终为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!。 没有机会,代码将在任何这些条件下结束。

elif (-4 != -4):
    print "Egad!"# Keep going here.
else:
     return True # You'll want to add the else statement, too!

这些条件绝对没必要。你唯一能做的就是改变第一个条件(例如使用一些变量,这将采用不同的值,因此每次条件不会是TRUE