I'm down to my last check50 frown (:( buy handles fractional, negative, and non-numeric shares expected status code 400, but got 200) but my code doesn't allow for these, so I don't know what to do. Everything else passes, and I really want to submit this with 100% smiley faces. Here is my html for buy, showing the constraints within the html code:

{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
    <form action="/buy" method="post">
        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="symbol" placeholder="Stock Symbol" type="text">
        <div class="form-group">
            <input class="form-control" name="shares" placeholder="Number of Shares" type="number" min="1">
        <button class="btn btn-primary" type="submit">Buy</button>
{% endblock %}

You cannot submit a number that isn't positive and whole. And you can't even type a letter except for e and even then, it's not a whole number, so you can't submit it. Why am I getting this check50 frown? I'll add the python code, as well, in case that helps to answer this.

@app.route("/buy", methods=["GET", "POST"])
def buy():
    """Buy shares of stock"""
    if request.method == "GET":
        return render_template("buy.html")
        # lookup stock -- return apology if not there
        if not request.form.get("symbol"):
            return apology("must provide stock symbol")
        elif not request.form.get("shares"):
            return apology("must provide number of shares")
            quote = lookup(request.form.get("symbol"))
            if quote == None:
                return apology("Cannot retrieve quote")
                # look up the price of the stock
                # SELECT cash from users
                available = db.execute("SELECT cash FROM users WHERE id=:user_id", user_id=session["user_id"])
                cash = available[0]["cash"]
                saleprice = int(quote["price"]) * int(request.form.get("shares"))
                stock = request.form.get("symbol")
                symbol = stock.upper()
                if saleprice > cash:
                    return apology("insufficient funds")
                    # create a new db table to store purchases and sold stock
                    db.execute("INSERT INTO receipts(user_id, stock_symbol, shares, timestamp, price, type) VALUES(:user_id, :stock_symbol, :shares, :timestamp, :price, :type)", 
                    user_id=session["user_id"], stock_symbol=symbol, shares=request.form.get("shares"), timestamp=datetime.utcnow().isoformat(), price=quote["price"], type="purchased")
                    # update available cash in users
                    change = cash - saleprice
                    db.execute("UPDATE users SET cash=:cash WHERE id=:user_id", cash=change, user_id=session["user_id"])
                    # update the current number of stocks owned
                    if db.execute("SELECT stock_symbol FROM current WHERE UPPER(stock_symbol)=:stock_symbol AND user_id=:user_id", 
                    stock_symbol=symbol, user_id=session["user_id"]):
                        new = db.execute("SELECT shares FROM current WHERE UPPER(stock_symbol)=:stock_symbol AND user_id=:user_id", stock_symbol=symbol, user_id=session["user_id"])
                        shares = new[0]["shares"] + int(request.form.get("shares"))
                        db.execute("UPDATE current SET shares=:shares WHERE user_id=:user_id AND UPPER(stock_symbol)=:stock_symbol",user_id=session["user_id"], shares=shares, stock_symbol=symbol) 
                    # Add row to table if first time purchasing stock
                        db.execute("INSERT INTO current(user_id, stock_symbol, shares) VALUES(:user_id, :stock_symbol, :shares)", 
                        stock_symbol=symbol, user_id=session["user_id"], shares=request.form.get("shares"))
                return redirect("/")

While it is arguably better UI/UX to validate the input in the html, it does not fulfill the requirements of the buy spec:

Require that a user input a number of shares, implemented as a text field whose name is shares. Render an apology if the input is not a positive integer.

  • Palm to the forehead. At least I got some experience doing it the other way! Thanks.
    – Lperino
    Sep 21 at 15:02

You must log in to answer this question.

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