0

I ran my code through check50 and it didn't clear the following 2 checks:

:( quote handles valid ticker symbol
expected to find "28.00" in page, but it wasn't found
:( buy handles valid purchase
expected to find "112.00" in page, but it wasn't found

I can't identify what are the particular problems with my code that I have to rectify. Appreciate it if someone can help me through. My code is as follows:

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    """Buy shares of stock"""
    # User reached via POST (by submitting a form)
    if request.method == "POST":
        # Get symbol of stock and number of shares that user wants to buy
        # Symbol and price values derived from lookup
        stock = lookup(request.form.get("symbol"))
        # Ensure user gives correct stock symbol
        if not stock:
            return apology("Please input a symbol!")

        # Ask for number of shares user wants to buy
        try:
            quantity = int(request.form.get("shares"))
        except ValueError:
            return apology("Please input valid no. of shares")
        # Ensure user gives valid no. of shares
        if not quantity or quantity<0:
            return apology("Please input valid no. of shares")

        #Check if user can afford stock
        userCash = db.execute("SELECT cash FROM users WHERE id= :id", 
        id=session["user_id"])
        userCash = float(userCash[0]["cash"])
        if (stock["price"]*quantity)>userCash or not userCash:
            return apology("You can't afford this stock!")

        # Select user shares of that symbol
        userQuantity = db.execute("SELECT quantity FROM portfolio WHERE id = 
    :id AND symbol=:symbol", \
        id=session["user_id"], symbol=stock["symbol"])

        # If user doesn't have shares of the symbol, create new stock object
        if not userQuantity:
            db.execute("INSERT INTO portfolio(user_id, symbol, price, quantity, val) VALUES(:user_id, :symbol, :price, :quantity, :val)",\
        user_id=session["user_id"], symbol=stock["symbol"], price=usd(stock["price"]),\
        quantity=quantity, val= (quantity*usd(stock["price"])) )
        # Else increment the shares count
        else:
            totalQuantity = userQuantity[0]["quantity"] + quantity
            db.execute("UPDATE portfolio SET quantity=:quantity WHERE id=:id AND symbol=:symbol", \
                    quantity=totalQuantity, id=session["user_id"], \
                    symbol=stock["symbol"])

        # Update user cash
        db.execute("UPDATE users SET cash = cash- :paid where id = :id ", paid=stock["price"]*quantity, id=session["user_id"])

        # Redirect user to home page
        return redirect("/")
    # If user reached via GET (by clicking a link)
    else:
        return render_template("buy.html")

@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
    """Get stock quote."""
    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":
        #Getting stock's symbol from user
        quote = lookup(request.form.get("symbol"))
        #Ensure user types into text field
        if not quote:
            return apology("Missing symbol!")

        return render_template("quoted.html", stock=quote)
    # User reached by GET
    else:
        return render_template("quote.html")
6
  • Are you formatting all your dollar values via usd, like {{ stock.price | usd }}? If not, you might be missing some digits, like 123.4 instead of 123.40. – Blauelf May 7 '18 at 9:03
  • Hello, I did format my dollar values but I am not sure where are the particular places that I have to do it. Do I have to format it wherever I used usd in application.py? I'm not exactly sure if we have to since usd returns a string and formatting the dollar values in the html pages would then become impossible? Thank you. – Prav Elan May 11 '18 at 12:09
  • I used usd only in the template (using the Jinja pipe syntax), and kept money as numbers everywhere else. – Blauelf May 11 '18 at 12:23
  • Oh, then am I right to say that you completely didn't use usd within application.py at all? Sorry to bother you so much but I'm really quite confused here. – Prav Elan May 11 '18 at 12:39
  • Well, it gets registered at some point so that I can use it in the templates. And I think I might have used in a flask flash message, which I have to construct in application.py. The thing I would do under no circumstances would be storing the money as formatted strings in the database. – Blauelf May 11 '18 at 13:02

You must log in to answer this question.

Browse other questions tagged .