功能文件:Python的 - isPrime和factList
def factList(p,n1):
counter = 1
while counter < n1:
if n1 % counter == 0:
p.append(counter)
counter = counter + 1
def isPrime(nbr):
counter1 = 1
factList(nbr)
while counter1 < nbr:
if len(nbr) == 2:
return True
else:
return False
counter = counter + 1
functiontester文件
lstt = []
n1 = 16
factList(lstt, n1)
print "The factors are of", n1, "are", lstt
nbr = 20
if isPrime(nbr):
print "Is Prime"
附:請勿刪除,更改或編輯因子列表的任何部分。 (即使它更簡單) 我正在努力使其能夠使用我的因子列表來確定我的號碼是否爲總數(只有一個數字)。如果你能幫助我做到這一點,請僅使用基本功能,就像我上面使用的基本功能。謝謝。
當我運行它,我得到 -
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
The factors are of 16 are [1, 2, 4, 8]
Traceback (most recent call last):
File "\\giant\home\2018\patelpre\FunctTstr.py", line 40, in <module>
if isPrime(nbr):
TypeError: isPrime() takes exactly 2 arguments (1 given)
>>>
顯然,您導入的'isPrime()'函數需要兩個參數,它與您在此處發佈的定義不匹配。 ** Tripple ** - 檢查你的假設,你沒有導入你認爲你正在導入的功能。 – 2014-12-06 21:24:18