2016-02-19 77 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

它还没有返回任何值,还有什么我可以做的吗? –