Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (18.4k points)

I'm trying to "execute" a (fake) buy order on a stock. I hit an error at the cash = cashcheck["cash"] line. It's saying that the indices must be integers, but we're talking about money, so it can't be int because there are decimals involved... or so I think.

Help is greatly appreciated!


@app.route("/buy", methods=["GET", "POST"])


def buy():

    """Buy shares of stock"""

    if request.method == "GET":

        return render_template("/buy.html")


        # collect user input - symbol

        symbol = request.form.get("symbol").upper()

        # if input is blank or symbol doesn't exist, return apology

        if not symbol:

            return apology("You must enter a stock symbol.", 300)

        # collect user input - # of shares

        shares = int(request.form.get("shares"))

        # if blank or not a positive integer, return apology

        if not shares:

            return apology("Enter a valid number of shares.", 300)

        # pull current price info from API

        quote = lookup(symbol)

        shareprice = quote["price"]

        totalprice = shareprice * shares

        # check users table to see how much cash user has

        cashcheck = db.execute("SELECT cash FROM users WHERE id = :userid", userid = session["user_id"])

        cash = cashcheck["cash"]

        if cash >= totalprice:

            # in transactions table, insert userID, symbol, shares, shareprice, and totalprice

            # transID should be autogenerated and autoincremented.  date is also autofilled by SQLite.

            db.execute("INSERT INTO transactions (userID, symbol, shares, shareprice, totalprice) VALUES (:userid, :symbol, :shares, :shareprice, :totalprice)",

            userid=session["user_id"], symbol=symbol, shares=shares, shareprice=shareprice, totalprice=totalprice)

            cash = cash - totalprice

            # update cash balance

            db.execute("UPDATE users SET cash = :cash WHERE id = :userid", userid = session["user_id"])

            #return index

            return ("/")


            # else, return apology (not enough cash)

            return apology("Not enough cash balance to make execute this order.", 300)


ERROR:application:Exception on /buy [POST]

Traceback (most recent call last):

  File "/usr/local/lib/python3.7/site-packages/flask/", line 2446, in wsgi_app

    response = self.full_dispatch_request()

  File "/usr/local/lib/python3.7/site-packages/flask/", line 1951, in full_dispatch_request

    rv = self.handle_user_exception(e)

  File "/usr/local/lib/python3.7/site-packages/flask/", line 1820, in handle_user_exception

    reraise(exc_type, exc_value, tb)

  File "/usr/local/lib/python3.7/site-packages/flask/", line 39, in reraise

    raise value

  File "/usr/local/lib/python3.7/site-packages/flask/", line 1949, in full_dispatch_request

    rv = self.dispatch_request()

  File "/usr/local/lib/python3.7/site-packages/flask/", line 1935, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

  File "/home/ubuntu/finance/", line 34, in decorated_function

    return f(*args, **kwargs)

  File "/home/ubuntu/finance/", line 86, in buy

    if cashcheck["cash"] >= totalprice:

TypeError: list indices must be integers or slices, not str

1 Answer

0 votes
by (36.8k points)
edited by

cash check is a list of rows that matched the query.

If you're only expecting one row to match, use cashcheck[0]["cash"].

Learn Data Science with Python Course to improve your technical knowledge.

Browse Categories