关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。
9个月前关门了。
改进这个问题
我想计算“totalstock”,但我得到了一个typeerror,因为p['stockvalue']不是整数。我尝试过使用int()函数,但它也不起作用。
def index():
# Get user ID
user_ID = session['user_id']
# Query for symbol, name, shares and store in variable
purchaseData = db.execute('SELECT symbol, name, shares FROM purchases WHERE id = :id', id=user_ID)
#Store all info in userData
userData = {}
stockCount = 0
for i in purchaseData:
userData['symbol'] = i['symbol']
userData['name'] = i['name']
userData['shares'] = i['shares']
userData['price'] = lookup(i['symbol'])
# Calculate stock value
userData['stockValue'] = lookup(i['symbol']) * i['shares']
# Count rows
stockCount += 1
# Query for remaining cash
userData['remainingCash'] = db.execute('SELECT cash FROM users WHERE id = :id', id=user_ID)
# Calculate total value
totalStock = 0
for p in userData:
totalStock = totalStock + int(p['stockValue'])
userData['totalValue'] = userData['remainingCash'] + totalStock
# Return render and send values
return render_template('index.html', rows=stockCount,**userData)
purchasedata的结构是什么?
1条答案
按热度按时间ui7jx7zq1#
这具有cs50金融项目的所有显著特征。根据规范:
lookup
,[是]一个函数,给定symbol
(例如nflx),以dict
有三个键:name
,其值为str
,公司名称;price
,其值为float
; 以及symbol
,其值为str
,股票符号的规范化(大写)版本,不管该符号在传入时如何大写lookup