0

I have tried a number of versions of this code, except I cannot get it to subtract shares from my ("/") index page it does nothing and shows nothing in the shares column in the transaction table. When I remove the -1 * shares it adds them. I also have a selling.html where it shows the same as buying.html i.e, the number of shares sold but never a -2 or -the amount of shares. Please advise if you have any suggestions.

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
if request.method=="POST":
     stock = lookup(request.form.get("symbol"))
     input_symbol = (request.form.get("symbol"))
     shares = request.form.get("shares")
     # Determine users stocks and shares in possession of current user from database #
     if stock == None:
         flash("The Stock Symbol Requested Does Not Exist")
     rows = db.execute("""SELECT symbol, SUM(shares) AS total_shares
                         FROM transactions
                         WHERE users_id = :users_id
                         GROUP BY symbol
                         HAVING total_shares > 0
                         """, users_id=session["user_id"])

    for row in rows:
        if (row["symbol"] == input_symbol):
            if (int(shares) > row["total_shares"]):
                flash("You Do Not Own or Have Enough Shares of This Stock to Sell")
                return apology("Review The Stock You Own Before Selling")


    type = ["sell"]
    price = stock["price"]
    stock_total = price * (int(shares))
    stock_name = stock["name"]
    cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])
    balance = cash[0]["cash"]
    current_time = dt.datetime.now()
    db.execute("""INSERT INTO transactions
                (users_id, symbol, shares, price, stock_total, time, type)
                VALUES (:users_id, :input_symbol, :shares, :price, :stock_total, :current_time, :type)"""
                , users_id=session["user_id"],
                  input_symbol=input_symbol,
                  shares= (-1 * shares),
                  price=price,
                  stock_total=stock_total,
                  current_time=current_time,
                  type=type)

    updated_cash = (balance + stock_total)
    total_value = (updated_cash + stock_total)
    db.execute("UPDATE users SET cash = :updated_cash WHERE id = :id", updated_cash=updated_cash, id=session["user_id"])
    flash("Sold")
    return redirect ("/")

else:
    rows = db.execute("""SELECT symbol
                  FROM transactions
                  WHERE users_id = :users_id
                  GROUP BY symbol
                  HAVING SUM(shares) > 0;
               """, users_id=session["user_id"])
    return render_template("/sell.html", symbols=[row["symbol"] for row in rows])'''
1

This shares = request.form.get("shares") creates shares as a string. -1 * shares returns '', because of the python "string multiplication" feature. Cast it to an int, as has been done here stock_total = price * (int(shares)). (Or cast it to an int when it is first set so the function will always treat it as an int.)

1
  • This suggestion worked terrifically! The compiler did not give me the "cannot combine a str with an int" error like it did with the stock_total variable; therefore, it would have taken me a while to figure out. I hope this explanation can help others having similar issues. – Christopher White Nov 30 '20 at 2:11

You must log in to answer this question.

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