0

I'm implementing the "sell" function and have a problem with the calculation of the new value for "share" and "cash".

Here is my code:

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""

    if request.method == "POST":

        stock = request.form.get("symbol")
        share = request.form.get("share")
        quote = lookup(stock)

        check_stock = db.execute("SELECT * FROM buy WHERE stock = :stock", stock = stock)

        if not check_stock:
            return apology("You have no stocks of this company")

        db.execute("UPDATE buy SET share = share - :new_share WHERE user_id = :user_id", new_share = share, user_id = session["user_id"])

        actual_price = quote["price"]

        db.execute("UPDATE users SET cash = cash + :new_value WHERE user_id = :user_id", new_value = actual_price * share, user_id = session["user_id"])

        return redirect("/")


    else:
        return render_template ("sell.html")

When I run my app, I notice the following strange behavior:

DEBUG:cs50:SELECT cash FROM users WHERE id = 4
DEBUG:cs50:SELECT * FROM buy WHERE user_id = 4
DEBUG:cs50:UPDATE buy SET share = share + '5' WHERE user_id = 4
DEBUG:cs50:UPDATE users SET cash = cash - 1740.95 WHERE id = 4

The compute-sign in the output are the opposite of the input..

What did I wrong?

Thanks for your help!

Cheers, Matthias

1 Answer 1

1

I suspect you copied buy.html to create sell.html form. I also suspect the form action was not updated to "/sell". The first query in the "strange" does not exist in this sell function. It would likely exist in the buy function, though.

1
  • Damn, stupid error.. thanks for your help!
    – Dawienchi
    May 31, 2019 at 18:15

You must log in to answer this question.

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