我從codewars.com有這個問題:前n個Primes數字。 雖然我沒有定義類Primes()和Primes.first(n1)的問題,但我需要在表單Primes.first(n1).last(n2)下找到最後的素數。而且我不知道如何定義最後(n2)而不會出現錯誤。Python - Class.function()。function()
import math
class Primes():
def first(self):
primes = []
count = 1
prime = True
while len(primes) != self:
for i in range(2, int(math.sqrt(count)) + 1):
if count % i == 0:
prime = False
break
if prime:
primes.append(count)
prime = True
count += 1
return primes
def last(self):
pass
,如果我嘗試Primes.first(5)。去年(3)我得到:AttributeError的: '名單' 對象有沒有屬性 '最後'。
請發佈您嘗試過的代碼和您收到的錯誤。謝謝。 – lrnzcig
我用代碼更新了我的任務。 –
你的代碼沒有意義,除了self以外,沒有任何參數可以用於'first'。所以,Primes()。first(3)並不代表什麼 –