2016-02-19 36 views
0

我是新手,我正在尋求幫助。我目前被困在一個我正在嘗試完成的程序中。這裏是:python函數不會返回基於條件的值

def printStock(stockList, stockPrice, p): 
    for i in range(len(stockPrice)): 
     if stockPrice[i] > p: 
      p = stockList[i] 
    print("The stocks with a higher value are:", p) 

def searchStock(stockList, stockPrice, s): 
    for i in range(len(stockList)): 
     if s == stockList[i]: 
      s = stockPrice[i] 
     elif s != stockList[i]: 
      s = -1 
    return s 

def mainFun(): 
    stockList= [] 
    stockPrice = [] 
    l = 1 
    while l > 0: 
     stocks = str(input("Enter the name of the stock:")) 
     stockList += [stocks] 
     if stocks == "done"or stocks == 'done': 
      l = l * -1 
      stockList.remove("done") 
     else: 
      price = int(input("Enter the price of the stock:")) 
     stockPrice += [price] 
     l = l + 1 
    print(stockList) 
    print(stockPrice) 
    s = input("Enter the name of the stock you're looking for:") 
    searchStock(stockList, stockPrice, s) 
    p = s 
    printStock(stockList, stockPrice, p) 

每次我運行程序到最後,它從不返回變量s出於某種原因。如果我用打印替換返回,它總是打印-1而不是stockPrice,如果它在列表中。我還收到一個錯誤,提示關於第3行的「無法編碼的類型int()> str()」。基本上,printStock函數接受三個參數,一旦函數完成,它應該打印出高於'p' 。 'p'的值與我在調用searchStock函數後得到的值相同,但我似乎無法使其工作。有人可以幫幫我嗎?

+0

歡迎StackOverflow的一切都應該工作。我已經針對您的問題提交了一些修改,特別針對一些英語語法(如大寫「I」這個詞)。希望你不會介意。 StackOverflow記錄了具有相同問題的未來用戶的問題,寫得越好,搜索引擎就越容易正確編制問題。你會發現你發佈的許多問題可能會被評論者調整,以便儘可能多的人能夠理解問題並幫助回答。 – srm

回答

0

s正從函數返回,但返回值沒有被分配給外部作用域上的任何變量。

只是

s=searchStock(stockList, stockPrice, s) 

更換

searchStock(stockList, stockPrice, s) 

和預期

+0

它還沒有返回任何值,還有什麼我可以做的嗎? –