0

I've seen a few CS50 Finance help questions regarding /index. I'm trying to get the route to display a user's owned stock information (share number, value, price, etc.). Right now it displays the correct share amounts but does not display the name, value, or price (all blank). The "cash" and "grandTotal" amounts display but grandTotal is not the correct amount. I think I'm just confused on how to access specific values in my returns.

Python/sqlite:

def index():
    """Show portfolio of stocks"""
    # sql queries to select stock info
    user_id = session["user_id"]
    stocks = db.execute("SELECT stock, symbol, SUM(shares) AS totalShares FROM purchases WHERE userid == :userid GROUP BY symbol", userid=user_id)
    currentCash = db.execute("SELECT cash FROM users WHERE id == :userid", userid=user_id)

    # Global variables to be updated
    tableInfo = []
    grandTotal = currentCash[0]["cash"]

    #Grabbing info from each owned stock
    for stockInfo in stocks:
        symbol = stocks[0]["symbol"]
        shares = stocks[0]["totalShares"]
        name = stocks[0]["stock"]
        currentStock = lookup(symbol)
        price = currentStock["price"]
        value = price * shares
        grandTotal += value

        tableInfo.append(stockInfo)

    # Display a table with portfolio info for current user
    return render_template("index.html", tableInfo=tableInfo, grandTotal=usd(grandTotal), currentCash=usd(currentCash[0]["cash"]))

HTML:

{% extends "layout.html" %}

{% block title %}
    Your Portfolio
{% endblock %}

{% block main %}
    <table class="table">
      <thead>
        <tr>
          <th scope="col">Stock</th>
          <th scope="col">Number of shares</th>
          <th scope="col">Current price</th>
          <th scope="col">Total value</th>
        </tr>
      </thead>
      <tbody>

            {% for stock in tableInfo %}

            <tr>
                <td>{{ stock.name }}</td>
                <td>{{ stock.totalShares }}</td>
                <td>{{ stock.price }}</td>
                <td>{{ stock.value }}</td>
            </tr>

            {% endfor %}

      </tbody>
    </table>


    <table class="table">
        <thead>
            <tr>
                <th scope ="col">Cash remaining</th>
                <th scope ="col">Grand total</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>{{ currentCash }}</td>
                <td> {{ grandTotal }}</td>
            </tr>
        </tbody>
    </table>

{% endblock %}
  • Welcome to Stack Overflow. Please read [ask] and try to explain your question more clearly. For example, you should start out by explaining *what problem the code is trying to solve*. "Regarding /index" doesn't tell us anything useful, because almost none of us are taking nor have taken your course (out of all the possible ones out there). The next thing is to explain *exactly what goes wrong* - it sounds like you are successfully serving a web page and seeing a result, but with the wrong number? What result do you get, and what should it be instead? – Karl Knechtel Jul 25 '21 at 19:15
  • Keep in mind that we also don't have access to your database, so unless your result makes no sense at all, we can't really verify that the problem is with your code. Are you sure that you don't just have weird values for the stock prices? – Karl Knechtel Jul 25 '21 at 19:17
  • " Right now it displays the correct share amounts but does not display the name, value, or price (all blank)" Well, where are you expecting those values to come from? (From the database? Which columns are you querying for?) When you do `for stockInfo in stocks:`, how are you expecting the `stocks` data to be structured exactly? Therefore what do you expect `stockInfo` to look like? Where you have `symbol = stocks[0]["symbol"]`, are you expecting that to do something different on each iteration? Why? – Karl Knechtel Jul 25 '21 at 19:21
  • Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and try to fix the problem yourself first. – Karl Knechtel Jul 25 '21 at 19:21
  • first you could use `print()` to see what you have in variable and which part is executed - it is called `"print debuging"`. – furas Jul 25 '21 at 20:19
  • as for me your `for`-loop is wrong. You get `for stockInfo in stocks:` but inside loop you doesn't use `stockInfo` to get symbols, etc. You use it only to do `tableInfo.append(stockInfo)` but you get the same result doing only `tableInfo = stocks.copy()` – furas Jul 25 '21 at 20:22
  • you should really use `print()` to see what you get from database. You use `"SELECT stock, symbol, SUM(shares) AS totalShares` so you get only `stock`, `symbol` and `totalShares` but not `name` and `price`. I thinky in template you should use `stock.stock` instead of `stock.name`. As for `value` and `price` - you would have to add these values to `for stockInfo in stocks:` like `stockInfo["price"] = ...` `stockInfo["value"] = ...` because `price = currentStock["price"]` `value = price * shares` creates only local variables which is not part of `stockInfo` – furas Jul 25 '21 at 20:36
  • I really appreciate the guidance here and I understand that my question was poorly written. I'm going to put some more time into the issue and come back with more specifics, if needed. Thanks again! – sjschutz Jul 26 '21 at 02:59
  • I just wanted to let you guys know that I already fixed the issue and now everything is displaying how it should. Used a combo of both your answers - going line by line and printing what my code was outputting. I'll delete the question in a bit but I wanted to say thank you! – sjschutz Jul 26 '21 at 04:23

0 Answers0