I receive the following error when I attempt to access the current cash from logged in user:

File "/home/ubuntu/workspace/pset7/finance/application.py", line 73, in buy
if (price * shares) > cash[0]['cash']:
IndexError: list index out of range

The above error code is referring to: cash[0]['cash'] . I obtain my value for variable cash with:

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

I thought that you can access the money amount using cash[value][key]. If so, why am I out of range?

When I use the following code (print("cash=",cash) right after my db.execute statement, I get the following in terminal: cash= [ ].

Also, I have verified that the user who is logged in has cash (10000.00) in 'cash'.

  • With the help of two awesome CS50 students, we figured out the problem. I needed to replace the double quotes around session["user_id"], with single quotes as: session['user_id']. – Flesheaters Mar 25 '17 at 18:00
  • This stupid problem is intermittent and keeps cropping up. Sometimes I can kill all processes and restart everything and it goes away. Other times it won't go away. As long as I get this error, I can't move forward because my code breaks at this line. – Flesheaters Mar 26 '17 at 15:16

I never found a satisfactory answer to this problem. Suffice it to say that if you receive any error, or make any changes to your SQL database by manually, then you need to close phpliteadmin, close the web server, close any open terminals, then restart flask, restart the web server. The error will go away.

You must log in to answer this question.

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