0

This is the code I have at the moment

dbshares = db.execute("SELECT :shares FROM portfolio WHERE user_id = :user_id", shares=shares, user_id=session["user_id"])
    if dbshares[0]['shares'] - shares < 0:
        return apology("you sell shares you don't have")

I believe the middle line is giving me trouble, and I'm not sure how it needs to be. I have tried all of the following

dbshares[0]-shares...
int(dbshares)-shares...
int(dbshares[0])-shares...
int(str(dbshares))-shares...

and such. If someone could help me out with what this is supposed to look like it would be appreciated, thanks!

1 Answer 1

0

Shot in the dark:

if dbshares[0]['shares'] - shares < 0:

The precedence order has "<" over "-". That means that "shares < 0" is being evaluated first. Maybe try adding parentheses?

if (dbshares[0]['shares'] - shares) < 0:
3
  • I gave it a try, the important part of the error message was ` File "/home/ubuntu/ide50/workspace/pset8/finance/application.py", line 264, in sell if (dbshares[0]['shares'] - shares) < 0: KeyError: 'shares'`
    – Joel Banks
    Commented Jun 8, 2019 at 21:29
  • Is your query working correctly? Does the query actually return the number of shares that the user owns, when he actually owns some? Also, does your code correctly handle the case where the query returns no rows, i.e., the user doesn't own the stock? And, of course, is that the correct, case sensitive column name?
    – Cliff B
    Commented Jun 8, 2019 at 23:00
  • Through lots of trial and error, I figured out the problem. dbshares = db.execute("SELECT :shares FROM portfolio WHERE user_id = :user_id", shares=shares, user_id=session["user_id"]) for some reason always gets the same value as we are trying to sell, not the amount from the database that we own. How would I get the value from the database of what the user owns?
    – Joel Banks
    Commented Jun 9, 2019 at 2:08

You must log in to answer this question.

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