【打印1到100之間的素數】

使用 for...else

def func():
    ans = []
    i = 2
    while i <= 100:
        for item in list(range(2, i)):
            if i % item == 0:
                i += 1
                break
        else:
            ans.append(i)
            i += 1
    return ans

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章