簡體   English   中英

我需要解決什么才能使此代碼返回“ True”?

[英]What do I need to fix to make this code return “True”?

這是我正在從事的項目的一些代碼:

# 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 < 4) and (-10 > -20)

好吧,3小於4。 -10大於-20 因此,這種表達永遠是正確的。 您的函數不執行return語句,因此返回None 您的函數可以這樣重寫:

def the_flying_circus():
    print "Hey now!"
    return None
# 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

此條件始終為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM