I felt like I could understand the for loops great in c, but seem to be hitting a wall here. Some generic conceptual questions before i get into it:

1) ANSWERED. what sequence does the program run, does it read HTML first or the Python scripts first?

2) ANSWERED. what is the difference between the for loop in HTML and for loop in python?


3) Main/In depth question: I can't seem to to get my sharesNow=lookup(share['symbol']) to loop. My sharesOwned in HTML are looping great, but not any thing else.

Index in python/flask:

def index():

    #get and consolidate user data in database 
    sharesOwned = db.execute("SELECT symbol, price, name, SUM(shares) AS quanties FROM shares WHERE id=:id GROUP by symbol, symbol ORDER by symbol", id=session["user_id"])

    #loop through "position" dict
    for share in sharesOwned:

            'pricey': sharesNow['price'],

    rows=db.execute("SELECT * FROM users where id = :id", id = session["user_id"])
    return render_template("index.html", sharesOwned=sharesOwned, position=position, name=rows[0]["username"])


{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}


        <li><h2> {{ name }}</h2></li>

<table class="table table-striped">

            <th>Share Symbol</th>
            <th>Shares Name</th>
            <th>Number of Shares</th>
            <th>Current price</th>
            <th>Total value</th>

         {% for share in sharesOwned %}
                <td>{{ share.symbol }}</td>
                <td>{{ position.name }}</td>
                <td>{{ share.quanties }}</td>
                <td>{{ position.pricey }}</td>
        {% endfor %}

{% endblock %}

Result: enter image description here

1 Answer 1


The for loop is working and is displaying exactly the data it has. And it technically (theoretically?) is identical to a for loop in C. Perform this code x times. Syntactically, you (normally) tell C how many times (eg for (i = 0; i < strlen(somestring); i++). The for stock in stocks notation is "syntactic sugar"; it knows how long stocks is, so it does the routine once for each element. You could also iterate over the list length-of-list times. The python syntax would be something like for i in range(0,length-of-list):

Hint: if you want to iterate through the sharesOwned list in the html, make sure everything you need is in that list.


  1. When you request index in the browser, the index route will execute and (eventually) call index.html at return render_template().
  2. The "difference" between the for loop in the html and the for loop in the python is that they are iterating over different objects (even though both objects are named sharesOwned). Each sharesOwned object is local to it's own "environment". The sharesOwned dict object from the index route is passed to the html(flask) as a parameter in the render_template call.
  3. In index: The position dictionary will only contain values from the last share row in the index route. What if sharesOwned had a key called "name" and a key called "pricey"? Something like share["name"] = sharesNow["name"] would do that. If you add a similar key for "pricey" (with the appropriate value, of course), then you only need to pass sharesOwned in the render_template call, and you can use share.name, share.pricey in the html. You could eliminate position. (You need to review the code the make sure you understand why it only has the values from the last share).
  • Thanks for your answers. So how do i make my stocks reflect the correct price of each share? Is lookup(share['symbol']) sufficient? It should technically loop through each share, right?
    – nvs0000
    Commented May 19, 2017 at 17:35
  • 1
    I added info to the answer (#3). You seem to fully understand for loops, but maybe are a little shaky on data/object structures. Commented May 19, 2017 at 19:29
  • I am pretty horrible at it aren't i.. Any idea where in the lectures/sites i can brush up on data/object structures? Python is nice, but confusing..
    – nvs0000
    Commented May 19, 2017 at 19:38
  • Thanks for everything @DinoCoderSaurus. So what i needed to do is to have the key inside of the sharesOwned(at this point is it an array or a dict?). I don't really understand why it only has values from the last share though.. Can you help me understand this?
    – nvs0000
    Commented May 19, 2017 at 20:23
  • I'm kind of stuck here too. Seem that should add the price key to shareowned. but how...I don't see why your code not working either... Commented Jun 8, 2017 at 7:17

You must log in to answer this question.

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