0

I am running into an issue with check50 on my buy function. Check50 gives me the following error:

:( buy handles valid purchase Cause application raised an exception (see the log for more details) Log sending POST request to /login sending POST request to /buy exception raised in application: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

This is my code:

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

        # Check for empty field
        if not shares:
            return apology('Enter number of shares you want to sell')
        if not stonk:
            return apology('Enter a stonk')

        # Check if stock exists
        if stonk == None:
            return apology('Please enter a valid stonk')

        # Check if shares is an integer
        try:
            shares = int(shares)
        except:
            return apology("Please enter a positive integer")

        # Check if shares > 0
        if shares <= 0:
            return apology("Please enter a positive number")

        # Get user id
        user = session["user_id"]
        # Get account balance
        balance = db.execute("SELECT cash FROM users WHERE id = ?", user)[0]["cash"]
        # Get price
        total = stonk['price'] * shares

        # Check to see if user has enough money
        if total > balance:
            return apology("You don't have enough money")

        # Update database
        db.execute("INSERT INTO transactions (user_id, transaction_type, stonk, stonk_price, shares, transaction_total) VALUES(?, ?, ?, ?, ?, ?)",
                   user, "Buy", stonk['symbol'], stonk['price'], shares, total)
        db.execute("UPDATE users SET cash = ? WHERE id = ?", (balance-total), user)

        # Redirect to home page
        return redirect("/")

    else:
        return render_template('buy.html')

It looks as though check50 is testing the function by submitting a blank field for the number of shares to be purchased. I believe that I am handling this correctly in the function, so I do not understand why this is bypassed. Check50 then states that an exception (TypeError) is raised by calling int(). I am also handling this, and check50 seems to have ignored this. When I test the website myself everything works fine. I'm only having issues with check50 and submit50.

I am super confused. Please advise me as to what I am doing wrong.

2
  • Is there an int() cast in index that does not handle exceptions? May 26 at 11:45
  • @DinoCoderSaurus there is no int() cast in index
    – alou64
    May 26 at 16:01
0

Not sure why it is not working in your code since you are using try but I had kind of similar problem and I fixed it by calling upon isnumeric() method.

You must log in to answer this question.

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