2014-12-06 30 views
0

功能文件: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) 
>>> 
+0

顯然,您導入的'isPrime()'函數需要兩個參數,它與您在此處發佈的定義不匹配。 ** Tripple ** - 檢查你的假設,你沒有導入你認爲你正在導入的功能。 – 2014-12-06 21:24:18

回答

0

基於代碼示例中,一個TypeError將由上使用isPrime的2線傳遞的僅1代替2參數到factList()引起的()函數。

在使用isPrime()函數,您嘗試只用一個整數使用factList()時,它需要一個列表和一個整數

factList(nbr) 

這將導致一個TypeError。您要麼希望isPrime()接受兩個參數(一個列表和一個整數)並將其傳入,或者要在isPrime()中創建一個空列表並將其傳遞到nbr的factList()中。

+0

你是什麼意思將「isPrime()中的那些傳入兩個參數(一個列表和一個整數)並將其傳入」,你可以在代碼中使用它嗎? – iPnL 2014-12-07 12:00:24

+0

我嘗試添加另一個參數並將其作爲列表,但現在它只是顯示爲false。這是我目前的代碼, – iPnL 2014-12-07 12:10:26

+0

謝謝我修復它,它的工作原理。 :d – iPnL 2014-12-07 12:30:59