我如何更正我的输出,以便它 returns 指定的类别与字典中相应的代码价格总和?

How do I correct my output so that it returns the specified categories with the corresponding tickers price sums inside the dictionary?

如何更正我的代码以便它对我指定的词典('Trash' 和 'Tech')进行分类。

然后加上每个代码每个类别的价格总和?

例如会输出:字典总和后的类别字符串。

Trash:(AMC + DOGE-USD,GME 的价格总和)

科技:(TSLA + NIO + NVDA 的价格总和)

import requests
import yfinance as yf

portfolios = {'Trash': ['AMC', 'DOGE-USD', 'GME'], 'Tech':['TSLA','NIO','NVDA']}

for tickers in portfolios:
    for tickers in portfolios[tickers]:
        info = yf.Ticker(tickers).info
        marketprice = str(info.get('regularMarketPrice'))
        message= tickers +","+ marketprice
        print(message)
        #print (portfolios)

您需要像这样修复循环:

for category in portfolios:
    sum = 0
    for ticker in portfolios[category]:
        info = yf.Ticker(ticker).info
        marketprice = info.get('regularMarketPrice')
        sum += marketprice
        message= ticker +","+ str(marketprice)
        print(message)
    print(category + ": " + sum)