I'm trying to implement index, and I'm having a bit of trouble. When trying to determine the total value of a user's holdings by doing the current price of each stock * the number of shares of stock, I got the error by the interpreter that I've put in the total of this question thread, "TypeError: can't multiply sequence by non-int of type 'float'".

The code might have other problems as well, so I'll just post all of my code for the route as well as the template as it is now.

index route:

def index():
    user_cash = db.execute("SELECT cash FROM users WHERE id=:id", id=session["user_id"])
    stock_symbol = db.execute("SELECT stock_symbol FROM transactions WHERE user_id=:user_id", user_id=session["user_id"])
    stock = lookup(stock_symbol[0]["stock_symbol"])
    if not stock:
        apology("symbol is invalid or something else went wrong")

    no_of_shares = db.execute("SELECT no_of_shares FROM transactions WHERE user_id=:user_id", user_id=session["user_id"])
    holdings = {
        "stock_name": stock["name"],
        "stock_symbol": stock["symbol"],
        "shares": no_of_shares,
        "stock_price": stock["price"],
        "total": stock["price"] * no_of_shares,
        "cash": user_cash

    return render_template("index.html", holdings=holdings)


{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
    <table class="table table-striped">
                <td colspan="4"></td>
                {% for holding in holdings %}
                    <td>{% holding.stock_price * holding.total %}</td>
                {% endfor %}
                {% for holding in holdings %}
                    <td>{% holding.stock_symbol %}</td>
                    <td>{% holding.stock_name %}</td>
                    <td>{% holding.shares %}</td>
                    <td>{% holding.stock_price %}</td>
                {% endfor %}
                <td colspan="4"></td>
                {% for holding in holdings %}
                    <td>{% holding.total %}</td>
                {% endfor %}
{% endblock %}

The problem is apparently in this line in the route code: "total": stock["price"] * no_of_shares.


The variable no_of_shares is probably not a number. As it's a return from a SQL SELECT statement, and assuming you are using the cs50 SQL object, it is probably a list of dictionaries. Based on this, you will probably have to say something like:

data = db.execute("SELECT no_of_shares FROM transactions WHERE user_id=:user_id", user_id=session["user_id"])
no_of_shares = data[0]['no_of_shares']

to access the number you want.

  • Pastebin paste for current state of index route: pastebin.com/Gw64H1ky; for template: pastebin.com/macgJ9AE. Could you please look at it and let me if it's fine for showing the info for any user logged in at any time, or if it's flawed for that? – Osman Zakir Mar 13 '17 at 20:43
  • It seems like doing it for more than one person at a time doesn't work. I have one account open on MS Edge, and when I opened the site on Chrome and tried to register as another user, I got a template error saying that holdings is undefined. – Osman Zakir Mar 13 '17 at 21:52
  • You need to narrow down your select statements to only select the data you need. I couldn't say why it doesn't work for multiple users. Go through your index route one line at a time, starting at the top, and make sure every line is doing what you need it to do by printing out the variables to the terminal or using debug50. Put temporary return apology statements in to prevent the code running all the way through and throwing errors you're not concerned with yet. Only when you're happy with one line, move onto the next. – Steve Bunting Mar 13 '17 at 22:42
  • If I only select what I need in the database query, like only taking information for the current user, will it be able to show information for any user logged in as needed? What I'm thinking is to try looping over the data in both tables and passing only the data for the user whose id=session["user_id"] into the template. Is that possible and feasible? I already printed holdings["total"] in another Python file and saw that it has the intended value and type. Or would it be okay to just select only the data for the current user (session["user_id"]) and only send that person's data in? – Osman Zakir Mar 14 '17 at 12:06
  • Didn't work well enough. Going into the site in another browser and buying shares of stocks as another user brings up errors. List index out of range and inability to add another row to the transactions table (the table where I'm storing info on who bought what at what price and when). pastebin.com/Gw64H1ky (index route); pastebin.com/macgJ9AE (index template). – Osman Zakir Mar 14 '17 at 14:32

You must log in to answer this question.

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