0

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'.

2
  • 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']. Mar 25, 2017 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. Mar 26, 2017 at 15:16

1 Answer 1

1

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.

1
  • Hey buddy! hope you are feeling good. I just find out solution to it. You just have to log out from Finance webapp and then try check50 again. Nov 9, 2022 at 8:27

You must log in to answer this question.

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