簡體   English   中英

為什么不打印這些功能?

[英]Why aren't these functions printing?

我過去寫的一些代碼有麻煩。 這些功能沒有在終端上打印任何內容是有原因的嗎?

def count_evens_2d():
    count = 0
    xss = [[10,13,17],[3,6,1],[13,11,12]]   
    for i in xss:
        for j in i:
            if j%2 == 0:
                count += 1
    print(count)

def min_2d():
    xss = [[10,13,17],[3,6,1],[13,11,12]]
    lowest_val = None
    for i in xss:
        for j in i:
            if lowest_val is None or j < lowest_val:
                lowest_val = j
                print(lowest_val)

我的邏輯似乎在第一個函數中試圖對列表列表中的所有偶數進行計數是正確的,而在第二個函數中,我試圖在列表中找到最小值。 感謝任何能提供幫助的人

您應該按照上述在代碼中調用它們。 例如:

def count_evens_2d():
    count = 0
    xss = [[10,13,17],[3,6,1],[13,11,12]]
    for i in xss:
        for j in i:
            if j%2 == 0:
                count += 1
    print(count)

def min_2d():
    xss = [[10,13,17],[3,6,1],[13,11,12]]
    lowest_val = None
    for i in xss:
        for j in i:
            if lowest_val is None or j < lowest_val:
                lowest_val = j
                print(lowest_val)


count_evens_2d()
min_2d()

我嘗試了一下,對於給定的列表列表,打印的值似乎正確。

暫無
暫無

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

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