Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 当列表中没有较高的股票时,如何添加“noHigherStocks”函数?_Python 3.x - Fatal编程技术网

Python 3.x 当列表中没有较高的股票时,如何添加“noHigherStocks”函数?

Python 3.x 当列表中没有较高的股票时,如何添加“noHigherStocks”函数?,python-3.x,Python 3.x,我试图创建一个有4个函数的程序,getStocks、searchStocks、printStocks,然后是使用其他三个函数的主函数。 我的问题是,我想这样做,如果你搜索的股票是最高的股票,我希望消息没有更高的股票出现作为输出,而不是空的空间,但我不确定我需要添加什么和在哪里这样做。我有noHigherStocks变量作为我想要显示的消息,但是我在哪里实现它呢?我觉得我应该在main函数中使用else语句,但是我想不出应该把它放在哪里。谢谢你的阅读!如有任何帮助或提示,将不胜感激:- def g

我试图创建一个有4个函数的程序,getStocks、searchStocks、printStocks,然后是使用其他三个函数的主函数。 我的问题是,我想这样做,如果你搜索的股票是最高的股票,我希望消息没有更高的股票出现作为输出,而不是空的空间,但我不确定我需要添加什么和在哪里这样做。我有noHigherStocks变量作为我想要显示的消息,但是我在哪里实现它呢?我觉得我应该在main函数中使用else语句,但是我想不出应该把它放在哪里。谢谢你的阅读!如有任何帮助或提示,将不胜感激:-

def getStocks():
    stockNames = []
    stockPrices = []
    name = str(input("What is the name of the stock?"))
    price = int(input("what is the price of that stock?"))
    while name != 'done':
        stockNames.append(name)
        stockPrices.append(price)
        name = str(input("What is the name of the stock?"))
        if name != 'done':
            price = int(input("what is the price of that stock?"))

    return (stockNames, stockPrices)


# returns a single value pertaining to the found price      
def searchStocks(stockNames, stockPrices, s):
    for i in range (len(stockNames)):
        if stockNames[i] == s:
            return stockPrices[i]
    return -1

# print the names of stocks whose price is higher than p.
def printStock(stockNames, stockPrices, p):
    i = 0
    while i <len(stockPrices):
        if p < stockPrices[i]:
            print(stockNames[i])
        i = i + 1
    return



def main():
    n,p = getStocks()
    stock = str(input("what stock are you searching for?"))
    price = searchStocks(n,p,stock)
    printStock(n,p,price)
    noStocksHigher = str('There are no stocks higher than',stock)

main()
这可以通过使用max builtin解决。因为你已经知道你搜索的股票的价格,你可以把这个价格和p中的最高价格进行比较,如果相等,打印你的消息,否则就搜索列表

从您调用searchStocks的位置开始:

price = searchStocks(n,p,stock)

if max(p) == price:

    print('There are no stocks higher than',stock)
else:

    printStock(n,p,price)

这应该可以解决问题。

我认为更改必须在printStock函数中,作为else语句,但每次尝试更改时,都会得到无效的语法。谢谢格式化帮助。我对这个编辑还很陌生,它的表现并不像我预期的那样。我感谢你的帮助。