0

Pset7 works perfectly: db writing , back-end calculations, front-end rendering - you name it. But check50, via cs50.me, complains:

enter image description here

Wheres this log ? There's no guidance as to which line, of which function, in which file, this is referring to. So Im hoping that the log being referred to is a real log which would give me those details, and not cs50.me, because that would be referring to itself - which is silly?

Where can I find the log being referred to? Or is cs50.me itself the log ? Or have I missed something?


                              The Buy Function

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    if request.method == "GET":
        return render_template("buy.html")
    if request.method == "POST":
        #pdb.set_trace()
        symbol = request.form.get('symbol').upper()
        stub = lookup(symbol)
        if stub is None:
            return apology("Invalid Symbol", 400)
        #pdb.set_trace()
        name, share_price, symbolo = stub
        no_shares_requested = request.form.get('shares')
        if not no_shares_requested.isdigit():
            return apology("Only digits above 0", 400)
        #pdb.set_trace()
        no_shares_requested = int(no_shares_requested)
        total_amount = no_shares_requested * share_price
        balance = db.execute("SELECT cash FROM users WHERE username = :username", username=session['username'])
        cash = balance[0].get('cash')
        viable_balance = cash - total_amount
        if viable_balance >= 0:
            balance = db.execute(
                "INSERT INTO portfolio(username, user_id, symbol, name, shares, price,total, date ) values (:username,(SELECT id from users WHERE users.username= :username), :symbol, :name, :shares, :price, :amount, CURRENT_TIMESTAMP)",
                price=share_price, symbol=symbol, shares=no_shares_requested, username=session['username'],
                amount=total_amount, name=name)
            done = db.execute("UPDATE users SET cash = :viable_amount WHERE username = :username",
                              viable_amount=viable_balance, username=session['username'])
            return index()
        else:
            return apology("no cash for that bud", 403)
1
  • Did you modify lookup to return an array or write a custom lookup that returns an array? check50 will return a dict from lookup, full stop. Dec 26 '18 at 19:43
1

There are no other logs. If you cannot repro in your app, this will take a little more sleuthing. The trick would be to find the condition(s) that causes check50 to give TypeError. Since this viable_balance = cash - total_amount is the only subtraction in the buy function, it's a good place to start.

The error is claiming that total_amount is a string. What could make it a string here total_amount = no_shares_requested * share_price? If either operand were a string, the * would return a string. It's not no_shares_requested since it is earlier ensured to be a number. So what about share_price?

It is set here name, share_price, symbolo = stub, so what would cause it to be a string? It would be a string if stub is a dict and not an array.

The inner workings of check50 are unknown and unkowable. But since the stock market is a moving target (mostly down these days!), it can be inferred that check50 uses some black-box version of lookup to control the data.

From the spec [emphasis added],

lookup, a function that, given a symbol (e.g., NFLX), returns a stock quote for a company in the form of a dict with three keys: name, whose value is a str, the name of the company; price, whose value is a float; and symbol, whose value is a str, a canonicalized (uppercase) version of a stock’s symbol, irrespective of how that symbol was capitalized when passed into lookup.

Therefore it can be assumed that check50's lookup will return a dictionary. Regardless of whether you have modified lookup or created a custom function, buy will need to handle a dict as the return type from lookup in order to pass check50.

1
  • your detective work is truly top notch, yes - my lookup function returns a tuple...turns out riiiigggght back at the very beginning of playing with the pset, I had been experimenting with JSON - it being my first JSON experience; and I left my experimentation in the code, and continued on my jolly way and forgot the whole episode. How you were able to deduct this is a case study in debugging -thank you!
    – amle
    Dec 27 '18 at 8:47
0

To access the check50 log, just add --log to the end of your check50 slug i.e for this problem it would be check50 cs50/2019/x/finance --log

You must log in to answer this question.

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