I've been stuck on buy function for PSET8 Finance for a long time now. I'm stumbling at the very beginning and can't seem to proceed further. The code error checks for incorrect or no inputs in the buy.html form. But when I try to do a check of sufficient cash, it yields an internal server error. I can't for the life of me understand where the mistake is. I think the error is in the last else condition in the code below. Any help would be appreciated. I'm getting the following error:

ERROR:application:Exception on /buy [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/pset8/finance/helpers.py", line 34, in decorated_function
    return f(*args, **kwargs)
  File "/home/ubuntu/pset8/finance/application.py", line 68, in buy
    elif buyshares <= 0:
TypeError: '<=' not supported between instances of 'str' and 'int'
INFO:werkzeug: - - [30/Jun/2020 01:02:07] "POST /buy HTTP/1.0" 500 -
INFO:werkzeug: - - [30/Jun/2020 01:02:07] "GET /static/styles.css HTTP/1.0" 200 -

Here is the code:

@app.route("/buy", methods=["GET", "POST"])
def buy():
    """Buy shares of stock"""
    if request.method == "GET":
        return render_template("buy.html")

    elif request.method == "POST":
        buysymbol = request.form.get("buysymbol")
        buyshares = request.form.get("buyshares")
        buystock = lookup(buysymbol)

        if not buysymbol or not buyshares:
            return apology("Enter valid symbol and/or shares", 403)
        elif not buystock:
            return apology("Symbol not found", 403)
        elif buyshares.isdigit() == False or buyshares <= 0:
            return apology("Enter valid number of shares", 403)

            userdata = db.execute("SELECT * FROM users WHERE id = :userid", userid = session["user_id"])
            usercash = userdata[0]["cash"]
            if buyshares.isdigit() == True or buyshares > 0:
                buyshares = int(buyshares)
            stockprice = buystock["price"]
            tradevalue = stockprice * buyshares
            if usercash < tradevalue:
                return apology("insufficient cash", 403)
  • an internal server error should be giving very specific error info (traceback) in the flask run terminal. Perhaps edit the question and add this traceback information. The code shown doesn't have a return on success. – DinoCoderSaurus Jun 30 '20 at 0:30
  • Thanks for your comment. I've updated the question with the error message I'm getting. I'm having a hard time deciphering it. I'd appreciate your help in helping me figure this out. – hrithikbansal Jun 30 '20 at 1:05
  • figured it out. needed to change the type/class for one of the variables. – hrithikbansal Jun 30 '20 at 1:21

You must log in to answer this question.

Browse other questions tagged .