0

I'm having issues figuring out why i'm running into this error. I've tried looking online and seeing what answers I could find but I'm stuck. Any help would be greatly appreciated.

Error is: :( buy handles valid purchase expected to find "112.00" in page, but it wasn't found

Code:

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    """Buy shares of stock"""
    if request.method == "POST":
        symbol = request.form.get("symbol").upper()
        item = lookup(symbol)

        if not symbol:
            return apology("Please enter a symbol")
        elif not item:
            return apology("Invalid symbol")

        try:
            shares = int(request.form.get("shares"))
        except:
            return apology("Shares must be an integer")

        if shares <= 0:
            return apology("Shares must be a positive integer")

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

        item_price = item["price"]
        total_price = item_price * shares

        if cash < total_price:
            return apology("not enough cash")
        else:
            date = datetime.datetime.now()

            db.execute("UPDATE users SET cash = ? WHERE id = ?", cash - total_price, user_id)
            db.execute("INSERT INTO transactions (user_id, shares, price, symbol, date) VALUES (?, ?, ?, ?, ?)", user_id, shares, item_price, symbol, date)

        return redirect("/")

    else:
        return render_template("buy.html")

Index HTML:

{% extends "layout.html" %}

{% block title %}
    Portfolio
{% endblock %}

{% block main %}
<table class="table">
    <thead>
      <tr>
        <th scope="col">Symbols</th>
        <th scope="col">Shares</th>
        <th scope="col">Price</th>
        <th scope="col">TOTAL</th>
      </tr>
    </thead>
    <tbody>
        {% for row in database %}
      <tr>
        <td>{{ row["symbol"] }}</td>
        <td>{{ row["shares"] }}</td>
        <td>{{ row["price"] | usd() }}</td>
      </tr>
      {% endfor %}
    </tbody>
    <tfoot>
        <tr>
            <td></td>
            <td></td>
            <th scope="1">Cash</th>
            <th scope="1">{{ cash | usd() }}</th>
        </tr>
    </tfoot>
  </table>
{% endblock %}

Buy HTML:

{% extends "layout.html" %}

{% block title %}
    Buy
{% endblock %}

{% block main %}
    <h1>Buy</h1>
    <form action="/buy" method="post">
        <div class="mb-3">
            <input autocomplete="off" autofocus class="form-control mx-auto w-auto" name="symbol" placeholder="Symbol" type="text">
        </div>
        <div class="mb-3">
            <input autocomplete="off" autofocus class="form-control mx-auto w-auto" name="shares" placeholder="Shares" type="text">
        </div>
        <button class="btn btn-primary" type="submit">Buy</button>
    </form>
{% endblock %}
2
  • Does index show "total value of each holding (i.e., shares times price)." (as required by the spec)? Commented Sep 3, 2022 at 21:37
  • Thank you @DinoCoderSaurus ! That is exactly what I was missing! Commented Sep 4, 2022 at 19:32

1 Answer 1

1

I have answer and I was going to paste just a link, but stack overflow is not allowing me to paste link or post answer if my message is not at least 30 characters. That is why you can see this message. )

Answer:

https://stackoverflow.com/a/77706367/20806031

2
  • please dont double post between here and stack overflow
    – UpAndAdam
    Commented Jan 2 at 17:32
  • also there is nothing in that answer that suggests its the same issue that is occurring there.
    – UpAndAdam
    Commented Jan 2 at 17:34

You must log in to answer this question.

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