0

I've just come back to CS50 after the semester ended and have been trying to debug my program to no avail. The application appears to be working when I test it myself, passes all checks, but has problems with two. Quote is not passing the "quote handles valid ticker symbol" check. I am not sure why it does not pass this check as when I use the program I have not run into any stock that I cannot get a quote for. Here is the code below:

@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
"""Get stock quote."""
if request.method == "POST":

    ## Retrieve symbol from user
    quote = lookup(request.form.get("symbol"))

    if not quote:
        return apology("Invalid stock symbol.")

    ## Display parameters of stock to user
    return render_template("quoted.html", stock = quote)

else:
    return render_template("quote.html")

Buy is having a similar issue, not passing the check "buy handles valid purchase". Likewise with quote, the program appears to function as expected but I have not been able to solve the problem. My guess is that it has something to do with a rounding error as it is looking for a specific number. Here is the code:

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""

if request.method == "POST":

    ## Ensure stock symbol was submitted and valid
    stock = lookup(request.form.get("symbol"))
    shares = request.form.get("shares")

    if not stock:
        return apology("Must provide a valid symbol")
    ## Ensure positive number of shares
    elif shares.isalpha() == True:
        return apology("Shares must be a positive integer")
    elif not shares.isdigit():
        return apology("Shares must be a positive integer")

    shares = int(float(shares))

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

    ## Variable to access rows in database
    row = db.execute("SELECT * FROM users WHERE id = :id", id = session.get("user_id"))

    ## Check users current cash
    balance = row[0]['cash']

    ## Check total required to buy shares against users cash
    total = stock['price'] * shares

    if balance < total:
        return apology("Insufficient funds")
    else:
        ## Update users cash
        db.execute("UPDATE users SET cash = :cash WHERE id = :id", cash = balance - total, id = session.get("user_id"))

        current_shares = db.execute("SELECT quantity FROM portfolio WHERE id = :id AND stock = :symbol", id = session.get("user_id"), symbol = stock['symbol'])

        ## Update Portfolio
        if not current_shares:
            ## Create new stock object
            db.execute("INSERT INTO portfolio (id, stock, price, quantity, total) VALUES (:id, :stock, :price, :quantity, :total)", \
            id = session.get("user_id"), stock = stock['symbol'], price = stock['price'], quantity = shares, total = total)
        else:
            db.execute("UPDATE portfolio SET quantity = :shares WHERE id = :id AND stock = :stock", shares = current_shares[0]['quantity'] + shares, id = session.get("user_id"), stock = stock['symbol'])

        ## Update History
        db.execute("INSERT INTO history (id, stock, price, quantity, timestamp) VALUES (:id, :stock, :price, :quantity, datetime('now'))", \
        id = session.get("user_id"), stock = stock['symbol'], price = stock['price'], quantity = shares)


    return redirect(url_for("index"))


else:
    return render_template("buy.html")

Any help would be much appreciated as the problem is nearly complete, but I cannot pass the checks for sell if I do not pass these two. Thank you in advance.

1

Your guess:

is that it has something to do with a rounding error as it is looking for a specific number.

is probably right. Usually using the 'usd' function in helpers.py will fix that right up. In quote you likely need to format price returned from lookup. The buy problem would likely be coming from index.

You must log in to answer this question.

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