0

I'm working on sell, and I ran into trouble in one of my db.execute statements. For whatever reason, in the previous line, it was perfectly fine using stock["price"] but in my second db.execute, it crashes and gives me an internal server error. Here is my code and thank you for your help

def sell():
"""Sell shares of stock"""
#return apology("TODO")
# PDHTEFW7T4KOTDHI
if request.method == "POST":

    shares = float(request.form.get("shares"))
    stock = lookup(request.form.get("symbol"))
    #stockprice = stock['price']
    if not stock:
        return apology("Invalid Symbol")

    if shares <= 0:
        return apology("shares must be > 0")

    db.execute("UPDATE users SET cash = cash + :amount WHERE id = :userID", amount=int(shares) * stock["price"], userID=session["user_id"])
    db.execute("INSERT INTO transactions (userID, symbol, shares, amount, stockprice) VALUES (:userID, :symbol, :shares, :amount, stockprice)", userID=session["user_id"], symbol = stock, shares = shares, amount = int(shares) * stock["price"], stockprice = stock["price"])

    return apology("good")

else:

    return render_template("sell.html")

1 Answer 1

1

You forgot the : for :stockprice in VALUES (:userID, :symbol, :shares, :amount, stockprice).


And to quote Cliff B:

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

7
  • I made this change, but I still get an internal server error. AttributeError: 'RuntimeError' object has no attribute 'name', did I mess up somewhere else?
    – Joel Banks
    Commented Oct 28, 2018 at 0:28
  • That part of the error message only says that the program which tried to catch the error had some problem. The "real" error message must be somewhere in there. Can you find the real error message? Commented Oct 28, 2018 at 7:50
  • Also, I feel that you try to put to many things in a single statement. Because of that, you compute several things 2 or 3 times. And because of that you missed one part of a calculation in one of the arguments for the second database call ... Breaking the code up in several lines makes it more readable and easier to maintain. But it also makes the error messages much more usable, because it tells you which calculation caused the error, in stead of than telling which group of 5 calculations caused it ... Commented Oct 28, 2018 at 7:54
  • File "/home/ubuntu/workspace/pset7/finance/helpers.py", line 34, in decorated_function return f(*args, **kwargs) File "/home/ubuntu/workspace/pset7/finance/application.py", line 248, in sell db.execute("INSERT INTO transactions (userID, symbol, shares, amount, stockprice) VALUES (:userID, :symbol, :shares, :amount, :stockprice)", userID=session["user_id"], symbol = stock, shares = shares, amount = int(shares) * stock["price"], stockprice = stock["price"]) is the most relevant part I can find. Where would you suggest breaking up that line?
    – Joel Banks
    Commented Oct 28, 2018 at 14:17
  • Ok, now we know which line casued the error. Now we need the actual error description, very near that line ... My main issue was that you are computing int(shares) * stock["price"] twice. If you put it in a variable, the computer only has to calculate it once. Besides, your code would become more readable. Commented Oct 28, 2018 at 14:34

You must log in to answer this question.

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