Almost finished Pset7 but I'm getting an error on Check50 that I don't understand. The log file says:
:( buy handles valid purchase
Log
sending POST request to /login
sending POST request to /buy
exception raised in application: TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
Here's my code for the index application:
def index():
"""Show portfolio of stocks"""
# User reached page via redirect
if request.method == "GET":
# Lookup username and cash of current user and store in variables
user = db.execute("SELECT username FROM users WHERE id = :user_id", user_id = session["user_id"])
cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])
# Lookup current portfolio of user and store in variable sumshares
sumshares = db.execute("SELECT stock, symbol, SUM(numshares), price, valuestocks FROM portfolio WHERE user_id = :user_id GROUP BY stock", user_id = session["user_id"])
# Define variable for total value of all stocks and initialize to zero
totalvaluestocks = 0
# Update sumshares with latest stock prices
for i in range(len(sumshares)):
sumshares[i]['price'] = lookupindex(sumshares[i]['symbol'])
sumshares[i]['valuestocks'] = sumshares[i]['price'] * sumshares[i]['SUM(numshares)']
totalvaluestocks = totalvaluestocks + sumshares[i]['valuestocks']
# Update to format USD
sumshares[i]['price'] = usd(sumshares[i]['price'])
sumshares[i]['valuestocks'] = usd(sumshares[i]['valuestocks'])
# Calculate grand total
grandtotal = cash[0]['cash'] + totalvaluestocks
# format values to USD
grandtotal = usd(grandtotal)
cash[0]['cash'] = usd(cash[0]['cash'])
totalvaluestocks = usd(totalvaluestocks)
# Generate index page
return render_template("index.html", user = user[0]['username'], cash = cash[0]['cash'], totalvaluestocks = totalvaluestocks, sumshares = sumshares, grandtotal = grandtotal)
# User reached route via POST (as by submitting a form via POST)
elif request.method == "POST":
# Check for valid input
try:
extracash = float(request.form.get("extracash"))
except ValueError:
return apology("Please type in valid cash amount", 403)
# Add cash to user
current = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])
db.execute("UPDATE users SET cash = :cash WHERE id = :user_id", cash = current[0]['cash'] + extracash, user_id = session["user_id"])
# Return user to home page
return redirect("/")
Looking at the log file, there is a problem with the application trying to multiply two variables, one of which is of type 'NonType' and the other is an int. Since there is only one line of code in my whole program where I multiply two values, one of them being an int it should result from:
sumshares[i]['valuestocks'] = sumshares[i]['price'] * sumshares[i]['SUM(numshares)']
This line of code is meant to update the value of the stock with the current price info. So instead of showing the historic price (at time of purchase), it shows the current price.
In the buy section there is another multiplication, but that multiplication has no ints as input.
Also the buy operation works as expected, when I type in an existing stock, a valid number of shares and have enough cash. Can someone help me out with a suggestion or knows where this error might be coming from?
lookup
twice, but only once, in case first succeeds and second fails. Anif
always ending inreturn
does not need anelse
, as anything after it already is reached only if thatif
did not trigger.