1

我的预期销售价值 (esv) 代码只会读取最近输入的股票,即如果我输入 gm 作为第一只股票,输入 ge 作为第二只股票,它只会读取 ge,因为它会覆盖 gm 的数据。我不确定如何为输入的每只股票计算 esv,因为它目前只计算输入的第一只股票。我的想法是它应该在输入每只股票后实际发生,并存储在一个新字典中,其中包含股票代码作为键,esv 作为值。但是,作业说这个过程应该发生在 GetSale 函数中......这使得它变得困难。以这种方式编码对我来说没有多大意义。无论如何,这是我的 GetSale 代码。

def getsale():
global names
global prices
global exposure
for key in names:
    symbol = key
    for key in exposure:
        risk_number = exposure[key][0]
        shares_held = exposure[key][1]
    for key in prices:
        purchase_price = prices[symbol][0]
        current_price = prices[symbol][1]
    esv = [-((current_price - purchase_price) - risk_number * current_price) * shares_held]
print("The estimated sale value of ", symbol, "is ", sorted(esv(),reverse = True))

编辑: 好的,我从另一个来源得到了答案。我需要创建一个新的空列表。此外,没有必要有多个 for 循环,因为它们都可以在一个循环中正常工作。然后,我只需要将 esv 和股票代码附加到我的列表中,然后对其进行排序/打印(我将其反转以便打印最高数字)。我会发布我自己问题的答案,但我需要等待几个小时。因此,这里是修改后的代码。

def getsale():
global names
global prices
global exposure
sellGuide=[]
for key in names:
    symbol = key
    risk_number = exposure[symbol][0]
    shares_held = exposure[symbol][1]
    purchase_price = prices[symbol][0]
    current_price = prices[symbol][1]
    esv = (float(((current_price - purchase_price) - risk_number * current_price) * shares_held))
    sellGuide.append([esv, symbol])
print(sorted(sellGuide, reverse = True))

但是,谁能告诉我一种只打印列表中第一个的方法?我认为这段代码可以工作:

    print(sorted(sellGuide[0], reverse = True))

但我收到以下错误:

  File "D:\Python\Python Modules\stock_portfolio.py", line 43, in getsale
print(sorted(sellGuide[0], reverse = True))
TypeError: unorderable types: float() < str()
4

1 回答 1

2

你的代码应该是

print(sorted(sellGuide, reverse = True)[0])

在您的示例中,您将获得 sellGuide 中的第一个元素并对其进行排序。所以你在一个 int/float 上运行排序,这是行不通的。

于 2013-04-29T03:18:15.457 回答