I am currently trying to complete finance, but got the "Internal Server Error" apology along with the KeyError in the title when trying to run index. Below is the error text I received and the application.py code for index with its respective html code. I also attached a screenshot of my database so you can look at my keys.

Error Result:

Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/pset8/finance/helpers.py", line 34, in decorated_function
    return f(*args, **kwargs)
File "/home/ubuntu/pset8/finance/application.py", line 60, in index
    price = float(quote['price_per_share'])
KeyError: 'price_per_share'

Python Code:

def index():
""""Portofolio of stocks"""
# Look up the current user
    users = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
    stocks = db.execute(
        "SELECT symbol, price_per_share, shares FROM transactions WHERE user_id = :user_id GROUP BY symbol HAVING shares > 0", user_id=session["user_id"])

    cash_remaining = users[0]["cash"]
    total = cash_remaining

    for stock in stocks:
        symbol = stock['symbol']
        share_num = stock['shares']
        quote = lookup(symbol)
        price = float(quote['price_per_share'])
        total_stock_value = float(price * share_num)
        total += total_stock_value

    return render_template("index.html", stocks=stocks, total=total, cash_remaining=cash_remaining)

HTML code:

{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}

    <h5 class="center">Portfolio</h5>
    <table class="table table-striped">
                <th>Price per Share</th>
                <th>Total Stock Value</th>
            {% for stock in stocks %}
                    <td>{{ stock.symbol }}</td>
                    <td>{{ stock.share_num }}</td>
                    <td>{{ stock.price | usd }}</td>
                    <td>{{ stock.total_stock_value | usd }}</td>
            {% endfor %}
    <h3>Cash: {{ cash_remaining | usd }}</h3>
    <h3>Grand Total: {{ total | usd }}</h3>
{% endblock %}

Database Screenshot:

enter image description here

Thanks for the help!


The only keys in the stocks dicts are symbol and shares shares, as was selected from the database. There is no key price. The for loop builds a variable called price, but then nothing is done with it.

  • Thank you so much for the help! That error is fixed, but unfortunately a KeyError appeared when I added my key price_per_share to the code. Do you know why this might be? Also, I updated the code and added a screenshot of my database to the post in order to make it more clear. – hekqte Sep 2 '20 at 0:46
  • now price_per_share is a key in stocks, but not in quote which is the result of lookup. – DinoCoderSaurus Sep 2 '20 at 9:38

You must log in to answer this question.

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