我需要編寫一個函數,它將查找負數的因子並將它們輸出到列表中。我會怎麼做?我可以讓我的功能做正數(見下文),但不是負數。查找負數因子的函數
#Finds factors for A and C
def factorspos(x):
factorspos = [1,-6];
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
factorspos.append(i)
print(i)
我試圖改變,從使循環計數它會從選擇1(下面的代碼)數量計值,但仍然沒有結果:(
#Finds factors for A and C
def factorspos(x):
factorspos = [int(-6),1];
print("The factors of",x,"are:")
for i in range(int(-6), x + 1):
if x % i == 0:
factorspos.append(i)
print(i)
我已經改變了CCO來一個固定的數字。
#Finds factors for A and C
def factorspos(x):
Cco = -6
factorspos = [int(Cco),1];
print("The factors of",x,"are:")
for i in range(int(Cco), x + 1):
if x % i == 0:
factorspos.append(i)
print(i)
return factorspos
感謝您告訴我們。你有問題嗎? – jonrsharpe
@ Sepy13:Cco的價值是多少?你真的不應該像你的函數一樣調用你的「返回值」。這不是基本的視覺效果(所以你實際上必須返回一些東西或者你的功能不起作用) –
@ Sepy13:閱讀這個:你有_have_返回'factorpos' –