Still working on index in pset 7. I am able to display details of the shares held by the user and their cash balance in a table. I have done this by iterating over their holdings in a portfolio table, calling lookup on each symbol and appending the details of lookup to the line. However I cant figure out how to add the value of all the users shareholdings to get a total value for shares held so that this can then be added to their cash balance to get their total equity (i.e. shares + cash). I think maybe the SQL SUM statement should be used but I cant see how to do this as the total current value of the shares are not in table (as mentioned above, they are being appended to the list. I have set out my code below. Any help would be greatly appreciated.
def index(): #Get holdings of the user holdings = db.execute("SELECT symbol, shares, price FROM portfolio WHERE id = :id", id = session['user_id']) for line in holdings: symbol = str(line["symbol"]) quote = lookup(symbol) line["name"] = (quote['name']) line["current_price"] = usd(quote['price']) line["value_shares"] = usd(line["shares"] * quote['price']) #get remaining cash for user cash = db.execute("SELECT cash FROM users WHERE id = :id", id = session['user_id']) quote_cash = usd(cash["cash"]) return render_template("index.html", holdings = holdings, quote_cash = quote_cash)