Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. def index():
  2. # STILL TO DO:
  3. # why aren't the lines for each stock showing up in page
  4. # continue testing for other bugs
  5.  
  6. # get user's stocks from stockPurchases: symbol, name, and total shares for each stock
  7. # assign above to stocks
  8. stocks = db.execute("SELECT stock,stockName,SUM(shares) FROM stockPurchases WHERE user = :user GROUP BY stock", user = session["user_id"])
  9.  
  10. # if the user hasn't yet bought stock
  11. if not stocks:
  12. return apology("Welcome to CS50 Finance, your best place to buy and sell stock")
  13.  
  14. # if the user does have a stock portfolio
  15. else:
  16. # initializing variable total outside of for loop
  17. total = 0
  18.  
  19. # for each stock that the user owns
  20. for stock in stocks:
  21. # assign variables to stock symbol, number of shares, and subsequent 'lookup'
  22. symbol = stock["stock"]
  23. share = stock["SUM(shares)"]
  24. theStock = lookup(symbol)
  25.  
  26. # assign current price of stock after lookup to price variable
  27. price = theStock["price"]
  28.  
  29. # determine current value of stock based on current price and number of shares
  30. stockTotal = price*share
  31.  
  32. # add the stock total value to a running total for all the stocks owned
  33. total += stockTotal
  34.  
  35. # get user's current cash value from users db and assign to rows
  36. rows = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
  37. # assign cash value to cash variable
  38. cash = rows[0]["cash"]
  39. # calculate user's total value adding both current cash value and current value of stocks
  40. grandTotal = cash+total
  41.  
  42. # render index template while passing in values for all the stocks, current cash, current stocks totals and the grandTotal
  43. 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