2012-11-02 46 views
0

python新增功能。所以我有這個功能,而且我不知道如何讓它取兩個參數並將它返回到另一個列表中。主要程序中的 我希望它調用x在列表中搜索,並將它發生的地方打印出來。 我是否以正確的方式開展工作? 這是我想出來的。 會真正感謝幫助 在此先感謝。我如何獲得一個函數來接受兩個參數?

def find_multiple(): 
    arg1 = input(" L: ") 
    arg2 = input(" x: ") 

    return L 

def main(): 
    L = [4, 10, 4, 2, 9, 5, 4 ] 
    x = int(input("Enter an element to search for in the list: ")) 
    if (len(L_indexes) == 0): 
     print(x, " does not occur in L.") 
     L =[] 
     results = L  

print("enter an element to search for in the list: ") 
if(len(L) == 0): 
    print("element does not occur in the list") 
else: 
    print("the number of occurrences in L: ", x) 

main() 
+2

你知道如何獲得一個函數來接受一個參數嗎? – JBernardo

+0

這可能是一個好主意,通過教程... http://docs.python.org/2/tutorial/controlflow.html#defining-functions – monkut

回答

1
def add(a, b): 
    return a + b 

編輯:根據你發佈什麼,這是我認爲你正在嘗試做的。

def search(myBigFancyX, myBigFancyList): 

    counter = 0 
    for number in myBigFancyList: 
     if number == myBigFancyX: 
      counter += 1 
    return counter 

if __name__ == "__main__": 

    l = [4, 10, 4, 2, 9, 5, 4 ] 
    x = int(input("Enter an element to search for in the list: ")) 

    occurances = search(x, l) 
    if occurances == 0: 
     print("element does not occur in the list") 
    else: 
     print("the number of occurrences in L: ", occurances) 
+0

啊是的,謝謝你。我不確定論據的去向。我曾經想過你在開始時在這裏發佈的內容,但後來我懷疑。謝謝,這是一個很大的幫助。 – noobie

+0

'搜索'似乎是寫'myBigFancyList.count(myBigFancyX)'的一個很長的路... ... –

+0

@JonClements是的,但我敢打賭,他的老師期待他使用原語,什麼都沒有。 – John

相關問題