I'm trying to implement index
, and I'm having a bit of trouble. When trying to determine the total value of a user's holdings by doing the current price of each stock * the number of shares of stock, I got the error by the interpreter that I've put in the total of this question thread, "TypeError: can't multiply sequence by non-int of type 'float'".
The code might have other problems as well, so I'll just post all of my code for the route as well as the template as it is now.
index
route:
@app.route("/")
@login_required
def index():
user_cash = db.execute("SELECT cash FROM users WHERE id=:id", id=session["user_id"])
stock_symbol = db.execute("SELECT stock_symbol FROM transactions WHERE user_id=:user_id", user_id=session["user_id"])
stock = lookup(stock_symbol[0]["stock_symbol"])
if not stock:
apology("symbol is invalid or something else went wrong")
no_of_shares = db.execute("SELECT no_of_shares FROM transactions WHERE user_id=:user_id", user_id=session["user_id"])
holdings = {
"stock_name": stock["name"],
"stock_symbol": stock["symbol"],
"shares": no_of_shares,
"stock_price": stock["price"],
"total": stock["price"] * no_of_shares,
"cash": user_cash
}
return render_template("index.html", holdings=holdings)
template:
{% extends "layout.html" %}
{% block title %}
Portfolio
{% endblock %}
{% block main %}
<table class="table table-striped">
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>TOTAL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="4"></td>
{% for holding in holdings %}
<td>{% holding.stock_price * holding.total %}</td>
{% endfor %}
</tr>
</tfoot>
<tbody>
<tr>
{% for holding in holdings %}
<td>{% holding.stock_symbol %}</td>
<td>{% holding.stock_name %}</td>
<td>{% holding.shares %}</td>
<td>{% holding.stock_price %}</td>
{% endfor %}
</tr>
<tr>
<td colspan="4"></td>
{% for holding in holdings %}
<td>{% holding.total %}</td>
{% endfor %}
</tr>
</tbody>
</table>
{% endblock %}
The problem is apparently in this line in the route code: "total": stock["price"] * no_of_shares
.