I'm working on pset 9 finance. I have my index nearly done but get an error when a new user logs in. Here is my code for index:
app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
# ask database for all transactions of user
user_buys = db.execute("SELECT * FROM TRANSACTIONS WHERE action = 'buy' AND user_id= ?", session["user_id"])
user_sells = db.execute("SELECT * FROM TRANSACTIONS WHERE action = 'sell' AND user_id= ?", session["user_id"])
# create dictionary of stock symbol and number of shares
totals = {}
# user_stocks is a list of dictionaries each with symbol, shares, price, value, cash, and grand total
user_stocks = []
# Dispay users cash
current_cash = get_cash(session['user_id'])
if not current_cash:
current_cash = 10000.00
# If the user has bought any stocks
if user_buys:
for buy in user_buys:
symbol = buy['stock']
shares = buy['shares']
# Add up number of shares for stocks bought by user
if symbol in totals:
totals[symbol] += shares
else:
totals[symbol] = shares
# if user does not own any stocks
else:
grand_total = get_cash(session['user_id'])
grand_total = usd(grand_total)
#If the user has sold any stocks
if user_sells:
for sell in user_sells:
symbol = sell['stock']
shares = sell['shares']
# Add up number of shares sold by user
if symbol in totals:
totals[symbol] -= shares
if totals[symbol] == 0:
totals.pop(symbol)
# if user owns stock then look up the current price of stock owned
if bool(totals):
for symbol, shares in totals.items():
lookup_result = lookup(symbol)
if lookup_result:
current_price = lookup_result['price']
# if can't find stock in lookup
else:
return apology("error in index page - lookup couldn't find price")
# finding value of stock by multiplying number of shares owned by the current price of that stock
total_value = current_price * shares
if total_value:
# adding all of the information about user stocks into list of dictionaries
info = {'symbol': symbol, 'shares': shares, 'current_price': current_price, 'total_value': total_value}
print(info)
user_stocks.append(info)
else:
return apology("No total value yet")
if user_stocks:
grand_total = 0
for user_stock in user_stocks:
grand_total += user_stock['total_value']
user_stock['total_value'] = usd(user_stock['total_value'])
grand_total = usd(grand_total + current_cash)
else:
grand_total = get_cash(session['user_id'])
grand_total = usd(grand_total)
return render_template("index.html", user_stocks=user_stocks, current_cash=usd(current_cash), grand_total=grand_total)
Here is the error message I got. It says RuntimeError("unsupported value: {}".format(value)) RuntimeError: unsupported value: {'id': 4}
. But I don't understand what part of my code is using that value in the index. Here is the entire Error message:
> * Running on
> https://kicks66rt-curly-garbanzo-9p9rjg9p4wr37x6w-5000.app.github.dev
> INFO: Press CTRL+C to quit INFO: * Restarting with stat ERROR:
> Exception on / [GET] Traceback (most recent call last): File
> "/usr/local/lib/python3.12/site-packages/flask/app.py", line 1455, in
> wsgi_app
> response = self.full_dispatch_request()
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 869, in
> full_dispatch_request
> rv = self.handle_user_exception(e)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 867, in
> full_dispatch_request
> rv = self.dispatch_request()
> ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 852, in
> dispatch_request
> return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/workspaces/127904154/finance/helpers.py", line 38, in
> decorated_function
> return f(*args, **kwargs)
> ^^^^^^^^^^^^^^^^^^ File "/workspaces/127904154/finance/app.py", line 47, in index
> user_buys = db.execute("SELECT * FROM TRANSACTIONS WHERE action = 'buy' AND user_id= ?", session["user_id"])
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/usr/local/lib/python3.12/site-packages/cs50/sql.py", line 29,
> in decorator
> return f(*args, **kwargs)
> ^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/cs50/sql.py", line 200, in
> execute
> _args = ", ".join([str(self._escape(arg)) for arg in args])
> ^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/cs50/sql.py", line 501, in
> _escape
> return sqlparse.sql.TokenList(sqlparse.parse(", ".join([str(__escape(v)) for v in value])))
> ^^^^^^^^^^^ File
> "/usr/local/lib/python3.12/site-packages/cs50/sql.py", line 497, in
> __escape
> raise RuntimeError("unsupported value: {}".format(value)) RuntimeError: unsupported value: {'id': 4} INFO: 127.0.0.1 - -
> [06/Dec/2023 19:49:50] "GET / HTTP/1.1" 500