0

Despite reading documentation I cannot figure out why my loop is not functioning properly and is only dynamic for stock symbol? Am I meant to be using multiple nested loops passing all prices, holding values, etc as dictionaries?

Please look at the below and any subtle guidance would be appreciated.

Python:

@app.route("/")
@login_required
def index():

    stocks = db.execute("SELECT DISTINCT stock FROM portfolio WHERE id = :id", id = (session["user_id"]))
    wealth = db.execute("SELECT SUM(number*price_current) FROM portfolio WHERE id = :id", id = (session["user_id"])) 
    wealth = wealth[0]
    wealth = wealth.get("wealth")
    cash = db.execute("SELECT cash FROM users WHERE id = :id", id = (session["user_id"]))
    cash = cash[0]
    cash = cash.get("cash")  
    for stock in stocks:
        symbol = stock.get("stock") 
        quote = lookup(symbol)
        price_current = int(quote["price"])
        db.execute("UPDATE portfolio SET price_current = price_current WHERE stock = :stock", stock = symbol)
        share_count=db.execute("SELECT sum(number) FROM portfolio WHERE stock = :stock", stock = symbol)
        share_count = share_count[0]
        share_count = share_count.get("sum(number)")
        holding = price_current*share_count
        return render_template("index.html", stocks = stocks, stock = stock, symbol = symbol, share_count = share_count, price_current = price_current,
holding = holding, cash = cash, wealth = wealth)

And then the plainest flask code (index.html) (I will only make it graphically more appealing after getting function right):

{% extends "layout.html" %}

{% block title %}
    Portfolio overview
{% endblock %}

{% block main %}

    {% for stock in stocks%}
        <p>{{ stock }}</p>
        <p>{{ symbol }}</p>
        <p>{{ share_count }}</p>
        <p>{{ price_current }}</p>
        <p>{{ holding }}</p>        
    {%endfor%}

    <p>{{ cash }}</p>
    <p>{{ wealth }}</p>    

{% endblock %}

2 Answers 2

1

If I understand this correctly, you are seeing the list of stocks but symbol, share_count, price_current and holding all display the values from the first stock. I think this "Am I meant to be using multiple nested loops passing all prices, holding values, etc as dictionaries? is partially true. I don't know as you need to use "multiple nested loops", or any nested loops, but if you had some kind of "array" structure (a list of dictionaries?) with one element per stock, and executed the "return" after the loop is complete (instead of after the first stock is processed), you might have better results.

1
  • Many thanks for your reply. Yes, that is exactly my issue - I get a list of different stocks but then price, value of each holding, and number of shares corresponding to each company is same across the board - the value of first stock in the list. I was too thinking of a list of dictionaries, where items in a list are dictionaries with attributes corresponding to each stock. Will try to implement now!
    – idol2k
    Commented Feb 9, 2017 at 22:07
0

As I've yet to do this one, I haven't read the task, but this looks wrong:

db.execute("UPDATE portfolio SET price_current = price_current WHERE stock = :stock", stock = symbol)

Maybe you meant

db.execute("UPDATE portfolio SET price_current = :price_current WHERE stock = :stock", stock = symbol, price_current = price_current)

You must log in to answer this question.

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