I'm stuck on pset7 and I have been for a while. I'm working on index and I'm pretty sure some of my variable names got mixed up, or I used the wrong names or something like that. I just can't figure out where I made a mistake. Please help me with this and point out anything else that needs fixed. The main problem is on the index table the middle 3 rows don't appear. Here's my code:
def index():
"""Show portfolio of stocks"""
# Once a user makes a purchase they will be redirected to their index page
# Edit that to reflect the user's portfolio
# Get symbol, shares, cash from databases
purchases = db.execute("SELECT stock, SUM(shares) as sumshares FROM transactions WHERE user_id = :user_id GROUP BY stock ORDER BY stock ASC", user_id = session["user_id"])
usercash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
usercash = usercash[0]["cash"]
# Declare variable for running total of stock value
totalvalstock = 0
# PDHTEFW7T4KOTDHI
# Purchases query will return dicts, loop to iterate over list of dicts.
for purchase in purchases:
stock = purchase["stock"]
shares = purchase['sumshares']
purchase['symbol'] = purchase['stock']
currentstock = lookup(purchase['stock'])
purchase['price'] = currentstock['price']
purchase['valbystock'] = purchase['sumshares'] * purchase['price']
totalvalstock += purchase['valbystock']
# calculate total of all stock holdings at their current price and whatever cash the user has left.
grandtotal = round(totalvalstock + usercash)
# call render_template and pass variables to html template
return render_template("index.html", purchase=purchases, usercash=usercash, grandtotal=grandtotal,)
Here's index.html
{% block title %}
Index
{% endblock %}
{% block main %}
<table width = 100%>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>Value</th>
</tr>
{% for purchase in purchases %}
<tr>
<td>{{purchase.symbol}}</td>
<td>{{purchase.name}}</td>
<td>{{purchase.price}}</td>
</tr>
{% endfor %}
<tr>
<td>Total Value</td>
<td> </td>
<td> </td>
<td> </td>
<td>{{grandtotal}}</td>
</tr>
<tr>
<td>Cash</td>
<td> </td>
<td> </td>
<td> </td>
<td>{{usercash}}</td>
</tr>
<tr>
<td>Total Assets</td>
<td> </td>
<td> </td>
<td> </td>
<td>{{grandtotal}}</td>
</tr>
</table>
{% endblock %}