Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def index():
- # STILL TO DO:
- # why aren't the lines for each stock showing up in page
- # continue testing for other bugs
- # get user's stocks from stockPurchases: symbol, name, and total shares for each stock
- # assign above to stocks
- stocks = db.execute("SELECT stock,stockName,SUM(shares) FROM stockPurchases WHERE user = :user GROUP BY stock", user = session["user_id"])
- # if the user hasn't yet bought stock
- if not stocks:
- return apology("Welcome to CS50 Finance, your best place to buy and sell stock")
- # if the user does have a stock portfolio
- else:
- # initializing variable total outside of for loop
- total = 0
- # for each stock that the user owns
- for stock in stocks:
- # assign variables to stock symbol, number of shares, and subsequent 'lookup'
- symbol = stock["stock"]
- share = stock["SUM(shares)"]
- theStock = lookup(symbol)
- # assign current price of stock after lookup to price variable
- price = theStock["price"]
- # determine current value of stock based on current price and number of shares
- stockTotal = price*share
- # add the stock total value to a running total for all the stocks owned
- total += stockTotal
- # get user's current cash value from users db and assign to rows
- rows = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
- # assign cash value to cash variable
- cash = rows[0]["cash"]
- # calculate user's total value adding both current cash value and current value of stocks
- grandTotal = cash+total
- # render index template while passing in values for all the stocks, current cash, current stocks totals and the grandTotal
- return render_template("index.html", stocks = stocks, theStock = theStock, cash = cash, total = total, grandTotal = grandTotal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement