Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

CS50 "Finance" problem, "Buy" function - Harvard CS50 course

$
0
0

Currently I am receiving the following errors for the "buy" section of the code. The code will run successfully and handles "buy" orders successfully, however check50 is returning these errors and I can't figure out why they are occurring or how to resolve them.

:( buy handles fractional, negative, and non-numeric shares    application raised an exception (see the log for more details):( buy handles valid purchase    expected to find "112.00" in page, but it wasn't found

Here is the code:

@app.route("/buy", methods=["GET", "POST"])    @login_required    def buy():"""Buy shares of stock"""    if request.method == "GET":        return render_template("buy.html")    else:        symbol = request.form.get("symbol")        shares = int(request.form.get("shares"))        if not symbol:            return apology("Must provide ticker")        stock = lookup(symbol.upper())        if stock == None:            return apology("Ticker does not exist")        if shares < 1:            return apology("Minimum purchase is 1 share")        transaction_value = shares * stock["price"]        user_id = session["user_id"]        user_cash_db = db.execute("SELECT cash FROM users WHERE id = :id", id=user_id)        user_cash = user_cash_db[0]["cash"]        if user_cash < transaction_value:            return apology("Not enough funds available")        uptd_cash = user_cash - transaction_value        # update the SQL database        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"], shares, stock["price"], date)        flash("Bought!")        # redirect to main page        return redirect("/")

And the 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="Ticker" type="text"></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">Buy</button></form>   {% endblock %}

I have tried using the isdigit() method instead of forcing the shared variable to be an int, but this creates a conflict when ensuring the value of the shares is an int which is greater than 0 which breaks the code.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>