1

I'm working on pset7 index, and I've hit a snag. When I try to log in and render index, I get this error: TypeError: 'NoneType' object is not subscriptable

It says the main error is on line 50: name = stock["name"] Here is my code:

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

#get users current stocks
holdings = db.execute("SELECT shares, symbol FROM history WHERE id = :id", id=session["user_id"])

#set placeholder for total cash value (cash + holdings)
cash = db.execute("SELECT cash FROM users WHERE id=:id", id=session["user_id"])
cash = cash[0]["cash"]
total_cash = cash

#get shares owned, current price, and total value of each holding
for holding in holdings:
    symbol = holding["symbol"]
    shares = holding["shares"]
    stock = lookup(symbol)
    name = stock["name"]
    current_price = stock["price"]
    total = current_price * shares
    total_cash += shares * current_price

#render template
return render_template("index.html", stocks=holdings, cash=usd(cash), total=usd(total_cash))

I'm guessing that the main problem is that symbol is returning a Nonetype, which means that the holdings variable isn't selecting properly. Does anyone have any ideas? Thank you.

1

see how you use cash:

cash = cash[0]["cash"]

however with holding you do the following:

symbol = holding["symbol"]
shares = holding["shares"]

the problem is in holding, which is an array, to iterate properly over holding we must do a little trick in the for loop.

i = 0
for holding in holdings:
    symbol = holding[i]["symbol"]
    shares = holding[i]["shares"]
    ........
    i = i + 1
3
  • I tried this but now it just throws an error that says "0". Holdings should be an array of dicts right? So I should be able to iterate over those (holding) and then access symbol and shares right? That's what's being returned for the db.execute command right? – Michael Nov 5 '17 at 20:21
  • not really a dictionary in the strict sense since the key-value pairs are repeated, that is, there are several keys "symbol" that is why we need an index i to obtain its value, for me it works well, you have to be careful with indentation , otherwise you publish the error with as much detail as possible to see if we can all help you, in reality this error appeared in pset7, but honestly I do not remember how to solve it, I think the fault was indentation – MARS Nov 5 '17 at 20:45
  • I mean the for loop for holding in holdings should iterate over each dict in the array already right? So I should be able to access those dicts key-values with holding["shares"] I tried using the iterator but it doesn't work. – Michael Nov 5 '17 at 21:01
0

I think you are getting the error because there is a problem with the lookup function(I'm guessing there's an issue with yahoo finance). I am having the same problem right now and I see that lookup returns nothing. I am trying it in quote also and I am getting an apology. So, in your case, stock is empty and stock["name"] and stock["price"] do not make sense to the python interpreter. Your solution is right and to continue through the rest of the pset you can just use the prices you have in the database until they fix this issue.

You must log in to answer this question.

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