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