0

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>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>{{grandtotal}}</td>
    </tr>
    <tr>
        <td>Cash</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>{{usercash}}</td>
    </tr>
    <tr>
        <td>Total Assets</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>{{grandtotal}}</td>
    </tr>
</table>
{% endblock %}
0

Yes, there is a problem with names. index.html iterates through a set named purchases here
{% for purchase in purchases %}.

But no variable named purchases is sent to index.html in this call
return render_template("index.html", stock=purchase["stock"], usercash=usercash, grandtotal=grandtotal)

There is a mis-match of names in index.html here:

{% for purchase in purchases %}

        <tr>
            <td>{{stock.symbol}}</td>
            <td>{{stock.name}}</td>
            <td>{{stock.price}}</td>
        </tr>

Pick a name and stick to it. In the return render_template send purchase=purchases. In the <td> elements use purchase..

That will get you closer. Other problems include (but might not be limited to):

  • In the index function, the purchases dictionaries only contain stock and sumshares (from the db.execute). If you want to display purchase.symbol and purchase.name in the html, you will have to "create" them. (something like purchase['symbol'] = purchase['stock'] in the for purchase in purchases: loop in application.py).
  • index.html has 5 <th> but the for loop only outputs 3 <tr>.
  • not using usd on the $ fields. This will be a problem with check50.

You are not that far off. Just remember the computer is very particular about what you "name" things. And when you make changes something, or something else, might/can/will go wrong. Slow and steady wins the race.

8
  • I made some of the changes, and I changed my code to reflect this. I added purchases to be sent in the return part, and tried changes it from stock to purchase but the variables don't show up still. What am I still doing wrong?
    – Joel Banks
    Sep 19 '18 at 19:53
  • I'll take your word for it that the py is changed from stock=purchase["stock"] to purchases=purchases in the render_template call. As for the html, inside the loop you would use purchase (the individual rows) as opposed to purchases (the whole array) in these lines <td>{{purchases.symbol}}</td> Sep 19 '18 at 21:05
  • I just changed what you said, but I'm still not getting anything for the middle three rows. I'm sorry to ask again I'm just confused to why it isn't working
    – Joel Banks
    Sep 19 '18 at 21:12
  • replace the code in the question with the currently running version. What do you mean "the middle three rows"? Sep 19 '18 at 21:33
  • updated, and I meant the three inside the purchases loop
    – Joel Banks
    Sep 19 '18 at 22:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .