0

I have been struggling with this problem set (pset7) for a couple weeks now and I have looked at countless resources while trying to get my code working. I am the point where I do not remember what is happening in my code at all. Could someone please explain what is actually getting stored in ledger? I know that the db.execute returns a list of some sort but how are the two fields (company and total_shares) related to each other now? Also, in the for loop, what is actually being iterated through and being stored where? I felt like had a pretty good grasp of these concepts before this pset but now I don't understand.

P.S. Please ignore the magic number, it is a placeholder for now.

def index():
    """Show portfolio of stocks"""
    current_cash_list = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
    cash = float(current_cash_list[0]["cash"])
    total = 8
    ledger = db.execute("SELECT company, SUM(number_purchased) AS total_shares FROM ledger WHERE user_id = :user_id GROUP BY company ORDER BY company", user_id=session["user_id"])

    for company in ledger:
        company = company["company"]
        total_shares = company["total_shares"]
        price_per_share = lookup(company)
        total_value = total_shares * price_per_share
    if request.method == "POST":
        return render_template("index.html", ledger=ledger, cash=cash, total=total, price_per_share=price_per_share)
    else:
        return render_template("index.html", ledger=ledger, cash=cash, total=total)
1

Could someone please explain what is actually getting stored in ledger?

Do you mean this ledger SELECT company, SUM(number_purchased) AS total_shares FROM ledger WHERE...... You can view the table structure in phpLiteAdmin (or sqlite3) to see what is stored in ledger table. Rows are (probably) created in the buy function, so review that to get more context.

Or do you mean this ledger ledger = db.execute("SELECT...? It is a list of dict objects, as described in Specification -> Hints.

I know that the db.execute returns a list of some sort but how are the two fields (company and total_shares) related to each other now?

They are keys representing the table's columns as described in Specification->Hints

Also, in the for loop, what is actually being iterated through and being stored where?

It is iterating through ledger (the variable). At each iteration, the next dict in ledger (ie the data from a row in the ledger table stored as key:value pairs) is stored in the variable company.

At least one problem is redeclaring company in the for loop.

for company in ledger: // company is an element from ledger, a dict object
        company = company["company"] // now company is a string, 'GOOG' perhaps?
        total_shares = company["total_shares"] // BUZZ, TypeError, since company is (now) a string
3
  • Thank you for the response. I am still a little confused with what is stored in ledger from the db.execute("SELECT.... I understand what is said in Spec-> Hints but my problem is the SUM portion and how it changes the data. Say I have three different transactions in my ledger for the same company. SUM will combine the number of shares into a single value, but will the three instances of the same company be consolidated to one that will be a key for the calculated SUM value? And for the for loop, I'm still pretty lost on that so I will read some documentation and stuff to figure it out. Thanks!
    – Ben
    Aug 28 '18 at 10:49
  • Try out your SQL in phpLiteAdmin (or sqlite3), without bind variables of course, and you can see the results, which certainly makes them easier to understand. Aug 28 '18 at 13:23
  • Thank you very much. All of this has given me a lightbulb moment! I'm pretty sure that I understand now.
    – Ben
    Aug 29 '18 at 11:10

You must log in to answer this question.

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