"unsupported format string passed to Undefined.format"--I know this happens because the non-numerical value is passed into the USD function, but I can't seem to spot the culprit.
As mentioned, index.html (and USD) works fine if accessed at any time NOT after buy is execute.
def index():
userid = session["user_id"]
"""Show portfolio of stocks"""
# Calculate the sum cash value of each share holding and update it in PORTFOLIO
cash_dict = db.execute("SELECT cash FROM users WHERE id = ?", session["user_id"])
owned_stocks = db.execute("SELECT symbol, name, price, sum(shares) AS sum_shares, price FROM portfolio WHERE id = ? GROUP BY symbol", userid)
if not owned_stocks:
return apology("Portfolio Empty", 400)
cash_one = cash_dict[0]["cash"]
cash = int(cash_one)
# Declare the stock+cash variable since declaring it inside the Python for loop will limit the variable scope to inside the for loop
total = int(cash)
# Calculate total values of stocks and cash - multiplying the total value of a particular share by its price and pre-adding user's cash ammount
for stock in owned_stocks:
total += int(stock["price"]) * stock["sum_shares"]
return render_template("index.html", owned_stocks=owned_stocks, cash=int(cash), total=int(total))
*******************
def buy():
"""Buy shares of stock"""
userid = session["user_id"]
if request.method == "POST":
symbol = request.form.get("symbol").upper()
# Ensure user input symbol
if not symbol:
return apology("Symbol No No")
# Get stock price
lookup_dict = lookup(symbol)
# Valid symbol?
if not lookup_dict:
return apology("Symbol doesn't exist")
# Stock price, name, and symbol
stock_price = lookup_dict["price"]
stock_symbol = lookup_dict["symbol"]
stock_name = lookup_dict["name"]
# Get shares number and convert to int, use an exception in case of error
try:
shares = int(request.form.get("shares"))
except:
return apology("Enter integer only")
# Ensure int is positive and not zero
if shares <= 0:
return apology("Positive number only")
# Get shares total price
total_price = shares * stock_price
# How much CASH user has?
cash_dict = db.execute("SELECT cash FROM users WHERE id = ?", userid)
# Since db.execute returns a LIST OF DICTS, I'll extract the int value from it
cash = cash_dict[0]['cash']
# Can user afford to buy the stocks?
if cash < total_price:
return apology("Not enough cash")
# Cash spent
current_cash = cash - total_price
print("!!!!!!!")
print(current_cash)
# Update USERS to reflect cash transaction
db.execute("UPDATE users SET cash = ? WHERE id = ?", current_cash, userid)
# Update PORTFOLIO
db.execute("INSERT INTO portfolio (user_id, symbol, shares, buy_or_sell, name, price) VALUES (?,?,?,?,?,?)", userid, symbol, shares, "purchase", stock_name, stock_price)
return render_template("index.html")
return render_template("buy.html")
*****************************
{% extends "layout.html" %}
{% block title %}
Index
{% endblock %}
{% block main %}
<table class="table table-striped">
<thead>
<th>Symbol</th>
<th>Name</th>
<th>Number of Shares</th>
<th>Price</th>
<th>TOTAL</th>
</thead>
<tbody>
{% for stock in owned_stocks %}
<tr>
<td>{{ stock["symbol"] }}</td>
<td>{{ stock["name"] }}</td>
<td>{{ stock["sum_shares"] }}</td>
<td>{{ stock["price"] | usd }}</td>
<!--multiply total number of each particular share by its price for each shares total stocks value!-->
<td>{{ (stock["sum_shares"] * stock["price"]) | usd }}</td>
</tr>
{% endfor %}
<tr>
<td colspan="4">Cash</td>
<td>{{ cash | usd }}</td>
</tr>
<tfoot>
<td colspan="4"></td>
<td> <strong> Grand Total: {{ total | usd }} </strong> </td>
</tfoot>
</tbody>
</table>
{% endblock %}