0

I completed problem set 9 and works as supposed, but with check50 I get the following result: :( sell handles valid sale expected to find "56.00" in page, but it wasn't found

For testing purposes, I even added the following lines of code in "helpers.py" in order to reflect transactions with the same resulting value showed in the error notification:

def lookup(symbol):
    """Look up quote for symbol."""
    if symbol == "AAAA" or symbol == "aaaa":
        return {"name": "Test A", "price": 28.00, "symbol": "AAAA"}
    # Contact API

Then I made buying and selling transaction which reflected the expected result, as shown in the pictures attached. Sell transaction History report

This is the code I used for the sell()

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "POST":
        
        # Ensure symbol is submited
        if not request.form.get("symbol"):
            return apology("symbol is required", 400)
            
        # Lookup price of stock for symbol provided    
        stock = lookup(request.form.get("symbol"))
        
        # Ensure quantity of shares is provided
        if not request.form.get("shares"):
            return apology("must indicate shares to sell")
        # Ensure shares are available in portfolio
        elif not request.form.get("shares").isdigit():
            return apology("invalid number of shares")
        symbol = request.form.get("symbol").upper()
        shares = int(request.form.get("shares"))
        stock = lookup(symbol)
        if stock is None:
            return apology("invalid symbol")
        
        # Lookup for available shares for selling
        rows = db.execute("""SELECT symbol, SUM(shares) as acciones FROM transactions WHERE user_id = :user_id
            GROUP BY symbol HAVING acciones > 0""", user_id=session["user_id"])
        for row in rows:
            if row["symbol"] == symbol:
                if shares > row["acciones"]:
                    return apology("you don't have enough shares")
                    
        # Lookup for cash and add value received from sell
        rows = db.execute("SELECT cash FROM users WHERE id=:id", id=session["user_id"])
        caja = rows[0]["cash"]
        shares = int(request.form.get("shares"))
        saldo = caja + shares * stock['price']
        
        # Update cash balance after transaction
        db.execute("UPDATE users SET cash=:saldo WHERE id=:id", saldo=saldo, id=session["user_id"])
        
        # Update transactions register
        db.execute("INSERT INTO transactions (user_id, symbol, shares, price) VALUES(:user_id, :symbol, :shares, :price)",
                   user_id=session["user_id"],
                   symbol=stock["symbol"],
                   shares=-1 * shares,
                   price=stock["price"])
        flash("SUCCESFULL TRANSACTION")
        return redirect("/")
        
    else:
        # Get shares available from transactions table
        rows = db.execute("""SELECT symbol FROM transactions WHERE user_id = :user_id GROUP BY symbol HAVING SUM(shares)>0;
        """, user_id=session["user_id"])
        return render_template("sell.html", symbols=[row["symbol"] for row in rows])

Can somebody tell me what is the problem with this code?

2
  • Try logging out in between buying and selling. Does it still work properly? – curiouskiwi Feb 20 at 5:12
  • I tried logging out between transactions and it works properly. – SeGue Feb 20 at 22:12

You must log in to answer this question.

Browse other questions tagged .