使用python将列出的元组中的值相乘

我需要有关使用具有列出值的元组的作业问题的帮助。

  1. 检查每个区块、将股票乘以购买价格并确定投资组合的总购买价格的函数。

  2. 第二个函数检查每个区块,将股票乘以购买价格和股票乘以当前价格,以确定获得或损失的总金额。

我们可以将每个股票块表示为具有购买日期、购买价格、股票、股票代码和当前价格的 5 元组。

portfolio= [ ( "25-Jan-2001", 43.50, 25, 'CAT', 92.45 ),

( "25-Jan-2001", 42.80, 50, 'DD', 51.19 ),

( "25-Jan-2001", 42.10, 75, 'EK', 34.87 ),

( "25-Jan-2001", 37.58, 100, 'GM', 37.58 )

]


温温酱
浏览 224回答 2
2回答

翻翻过去那场雪

计算成本(增加读取其他变量的数量,然后添加总数,可以进行循环但在移动设备上进行编辑)x = portfolio[0]res = 1for i in x[1:3]:  res *= i抱歉,我对#2 无能为力。

萧十郎

这是我写的代码。抱歉,缩进不正确,我从 PyScripter 中的文档中复制。投资组合= [ ( "25-Jan-2001", 43.50, 25, 'CAT', 92.45 ),( "25-Jan-2001", 42.80, 50, 'DD', 51.19 ),( "25-Jan-2001 ", 42.10, 75, 'EK', 34.87 ),( "2001 年 1 月 25 日", 37.58, 100, 'GM', 37.58 )]总购买价格=0起点=投资组合[0]投资组合中的起点:SharesxPurchasePrice= StartPoint[2]*StartPoint[1]TotalPurchasePrice=TotalPurchasePrice+SharesxPurchasePriceprint("投资组合的总购买价格:", TotalPurchasePrice)总购买价格=0TotalCurrentPrice=0起点=投资组合[0]对于投资组合中的起点:SharesxPurchasePrice= 起点[2]*起点[1]TotalPurchasePrice=TotalPurchasePrice+SharesxPurchasePrice对于投资组合中的起点:SharesxCurrentPrice=起点[2]*起点[4]TotalCurrentPrice=TotalCurrentPrice+SharesxCurrentPriceprint("投资组合的当前总价格:", TotalCurrentPrice)Total=TotalCurrentPrice-TotalPurchasePriceprint("总收益/损失:", Total)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python