0

Like many others before me, I am stuck at the sell error in pset 9 - finance

:( sell handles valid sale
Cause
expected to find "56.00" in page, but it wasn't found
Log
sending GET request to /signin
sending POST request to /login
sending POST request to /sell
checking that "56.00" is in page

Here's my sell function

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "GET":
        user_id = session["user_id"]
        symbols_user = db.execute("SELECT symbol FROM transactions WHERE user_id = ? GROUP BY symbol HAVING SUM(shares) > 0", user_id)
        return render_template("sell.html", symbols = [row["symbol"] for row in symbols_user])

    else:
        symbol = request.form.get("symbol")
        shares = int(request.form.get("shares"))

        if not symbol:
            return apology("Enter a valid stock symbol")

        stock = lookup(symbol.upper())

        if stock == None:
            return apology("Enter a valid Stock Symbol")

        if shares < 0:
            return apology("Enter a positive value")

        price = float(stock["price"])

        transaction_value = shares * price

        user_id = session["user_id"]
        user_cash_db = db.execute("SELECT cash FROM users WHERE id = ?", user_id)
        user_cash = user_cash_db[0]["cash"]

        user_shares = db.execute("SELECT shares FROM transactions WHERE user_id = ? AND symbol = ? GROUP BY symbol", user_id, symbol)
        user_shares_real = user_shares[0]["shares"]

        if shares > user_shares_real:
            return apology("Buy More Shares")

        uptd_cash = user_cash + transaction_value

        db.execute("UPDATE users SET cash = ? WHERE id = ?", uptd_cash, user_id)

        date = datetime.datetime.now()
        db.execute("INSERT INTO transactions (user_id, symbol, shares, price, date) VALUES (?, ?, ?, ?, ?)", user_id, stock["symbol"], (-1)*shares, price, date)

        flash("Stock Sold!")

        return redirect("/")

And here's my sell HTML file

{% extends "layout.html" %}

{% block title %}
    Sell
{% endblock %}

{% block main %}
    <h3>Sell</h3>
    <form action="/sell" method="post">
        <div class="mb-3">
            <select name="symbol">
                {% for symbol in symbols %}
                    <option value="{{ symbol }}">{{ symbol }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="mb-3">
            <input autocomplete="off" autofocus class="form-control mx-auto w-auto" name="shares" placeholder="Shares" type="number">
        </div>
        <button class="btn btn-primary" type="submit">Sell</button>
    </form>
{% endblock %}
0

0

You must log in to answer this question.

Browse other questions tagged .