0

The code was working before.

index function

@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""
    # User reached route via POST (as by submitting a form via POST)

    # get current symbol from global variable
    current_symbol = session["user_symbol"]
    current_id = session["user_id_2"]
    ten_thousand = 10000
    # only execute from buy route
    if len(current_symbol) != 0:

        stock = current_symbol
        stocks = lookup(stock.upper())

        db_stock_info = db.execute("SELECT stock_symbol, stock_name, shares, stock_price, stock_total FROM stock_info WHERE id = :present_id",
        present_id = current_id)

        db_id = db.execute("SELECT id FROM stock_info WHERE id = :present_id",
        present_id = current_id)


        # get cash_before_purchase
        db_total_cash = db.execute('SELECT total_cash FROM users WHERE id = :present_id',
        present_id = current_id)

        # get cash after purchase
        db_cash_after_purchase = db.execute("SELECT cash_after_purchase FROM users WHERE id = :present_id",
        present_id = current_id)

        # stock_price * shares
        db_stock_total = db.execute("SELECT stock_total FROM stock_info WHERE id = :present_id AND stock_symbol = :present_symbol"
        ,present_id = current_id, present_symbol = current_symbol )
        # pass on to sell.html
        db_stock_symbol = db.execute("SELECT stock_symbol FROM stock_info WHERE id = :present_id",
        present_id = current_id)


        # display id
        # displays stock_info = symbol, name, shares, price, total (can be more than 1 stock)
        # displays  total_cash iow cash_after_purchase
        # displays  stock_total + cash_after_purchase

        db_shares = db.execute("SELECT shares FROM stock_info WHERE stock_symbol = :not_db_symbol and id = :present_id",
        not_db_symbol = symbol, present_id = current_id)

        print( len(db_shares )

        return render_template("index.html", ten_thousand = ten_thousand, len_current_id = len(db_id), db_stock_info = db_stock_info,
        total_cash = db_total_cash[0]["total_cash"],
        cash_after_purchase = db_cash_after_purchas[0]["cash_after_purchase"],stocks = stocks)

    # not buy route
    else:
        db_total_cash = db.execute('SELECT total_cash FROM users WHERE id = :present_id',
        present_id = current_id)

        # get cash after purchase
        db_cash_after_purchase = db.execute("SELECT cash_after_purchase FROM users WHERE id = :present_id",
        present_id = current_id)



        return render_template("index.html", total_cash = db_total_cash[0]["total_cash"],
        cash_after_purchase = db_cash_after_purchase[0]["cash_after_purchase"])



<!–– https://www.geeksforgeeks.org/python-using-for-loop-in-flask/  -->

index.html

{% extends "layout.html" %}

{% block title %}
    index
{% endblock title %}

{% block main %}




<table>
  <tr>
    <th> SYMBOL </th>
    <th> NAME </th>
    <th> SHARES </th>
    <th> PRICE </th>
    <th> TOTAL </th>
  </tr>

<!-- if db_shares == 0 -->
{% if cash_after_purchase != ten_thousand %}



<!-- call rows -->
  {% for i in range(len_current_id) %}
  <tr{{[i]}}>

    <th> {{db_stock_info[i]["stock_symbol"]}} </th>
    <th> {{db_stock_info[i]["stock_name"]}}  </th>
    <th> {{db_stock_info[i]["shares"]}}    </th>
    <th> {{db_stock_info[i]["stock_price"]}} </th>
    <th> {{db_stock_info[i]["stock_total"]}} </th>
    {% endfor %}
  </tr>

{% endif %}


    </tr>
      <th> </th>
      <th> </th>
      <th> </th>
      <th> </th>
      <th> {{ cash_after_purchase}} </th>
    <tr>
        <th> </th>
        <th> </th>
        <th> </th>
        <th> </th>
        <th> {{ total_cash}} </th>
    </tr>

</table>

{% endblock %}

buy function

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    """Buy shares of stock"""

    if request.method == "POST":

        symbol = request.form.get("symbol")

       # Remove Space at the Start and end of the string in Python  :
        upper_case_symbol = symbol.upper()
        # http://www.datasciencemadesimple.com/remove-spaces-in-python/
        symbol = upper_case_symbol.strip()

        # why moving this up makes it work moving the codes down make it not work?


        """check for errors"""

        if not symbol or symbol == None:
            return apology("Stock symbol is inputted incorrect or blank.", 403)

        # confused by not
        if not request.form.get("counter"):
            return apology("you didn't click shares")


        """ use lookup function"""
        stocks = lookup(symbol)
        user_price =  stocks["price"]
        user_symbol = stocks["symbol"]
        user_fullname = stocks["name"]

        not_db_shares = request.form.get("counter")

        current_id = session["user_id"]

        """ put this here because want to count shares before insert
         may come back empty as in 0 that works"""
        db_shares = db.execute("SELECT shares FROM stock_info WHERE stock_symbol = :not_db_symbol and id = :present_id",

        not_db_symbol = symbol, present_id = current_id)

        db_stock_price = db.execute("SELECT stock_price FROM stock_info WHERE id = :user_id AND stock_symbol = :not_db_symbol",
        user_id  = current_id, not_db_symbol = symbol)

        """ already have at least one share also calculate new_total_stock_price and new total shares"""
        # https://stackoverflow.com/questions/3289601/null-object-in-python
        if len(db_shares) > 0:

            # stock_total                        193.34           1                                   193.34                    1
            stock_total = (user_price * int(not_db_shares)) + (db_stock_price[0]["stock_price"] * db_shares[0]["shares"])

            not_db_shares = db_shares[0]["shares"] + int(not_db_shares)

            # update shares
            db.execute("UPDATE stock_info SET shares = :present_shares WHERE id = :present_id AND stock_symbol = :not_db_symbol",
            present_shares = not_db_shares ,present_id = current_id, not_db_symbol = symbol)

        """calculate new_total_stock_price if have 0 shares"""
        else:
            stock_total = user_price * int(not_db_shares)

        db_total_cash = db.execute('SELECT total_cash FROM users WHERE id = :present_id',
        present_id = current_id)

        """can the user afford the stock"""
        # another error checker
        if db_total_cash[0]["total_cash"] < stock_total:
            return apology("You don't have enough money.",403)
         # update stock_total
        db.execute("UPDATE stock_info SET stock_total = :present_stock_total WHERE id = :present_id AND stock_symbol = :not_db_symbol",
        present_id = current_id, present_stock_total = stock_total, not_db_symbol = symbol)

        """calculate cash_after purchase"""
        cash_after_purchase = db_total_cash[0]["total_cash"] - stock_total


        db.execute("UPDATE users SET cash_after_purchase = :update_cash_after_purchase WHERE id = :present_id",
        present_id = current_id, update_cash_after_purchase = cash_after_purchase)

        """add all information in stock_info this is when adding brand new info into a database but may have same id"""

        if len(db_shares) == 0:


            db_symbol = db.execute("INSERT INTO stock_info (id,stock_name, stock_symbol, shares, stock_price, stock_total)\
            VALUES(:present_id, :name_stock, :user_symbol, :total_shares, :single_stock_price, :total_price)",
            present_id = current_id,
            name_stock = user_fullname,
            user_symbol = symbol,
            total_shares = not_db_shares,
            single_stock_price = user_price,
            total_price = stock_total)




        """ pass on stock_symbol to index using global variable """
        db_symbol = db.execute("select stock_symbol from stock_info where id = :present_id",
        present_id = current_id)

        # global variable
        session["user_symbol"] = db_symbol[0]["stock_symbol"]
        session["user_id_2"] = current_id
        return redirect ("/")

    else:
        return render_template("buy.html")

error message

     Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 303, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 291, in execute
    application_iter = app(environ, start_response)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 338, in __call__
    self._flush_bg_loading_exception()
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 326, in _flush_bg_loading_exception
    reraise(*exc_info)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 314, in _load_app
    self._load_unlocked()
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
File "/home/ubuntu/workspace/pset7/finance/application.py", line 108
    return render_template("index.html",ten_thousand=ten_thousand, len_current_id = len(db_id), db_stock_info = db_stock_info,
         ^
SyntaxError: invalid syntax
192.168.133.245 - - [08/Aug/2019 18:25:41] "GET /favicon.ico HTTP/1.0" 500 -
Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 303, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 291, in execute
    application_iter = app(environ, start_response)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 343, in __call__
    rv = self._load_unlocked()
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
File "/home/ubuntu/workspace/pset7/finance/application.py", line 108
    return render_template("index.html",ten_thousand=ten_thousand, len_current_id = len(db_id), db_stock_info = db_stock_info,
         ^
SyntaxError: invalid syntax

1 Answer 1

3

Sometimes it's the line before the line that is reported. That is the case here. This line print( len(db_shares ) has unbalanced parens.

2
  • Another question in the buy function there is a error. It is right before the else. It is caused by this line why? """calculate new_total_stock_price if have 0 shares""" Removing this line seems to do the job. @DinoCoderSaurus Also how do I get back the red dot that appears when you make an error in the ide?
    – grggthwrth
    Aug 9, 2019 at 4:34
  • 1
    Because everything that is indented after the if is considered part of the if clause. Since the comment line is not indented the if clause is "over", so the interpreter considers the else as a new clause that doesn't have an if. Either indent it or move it after the else:. If the linter (the red dot) is missing, you should contact [email protected]. Aug 9, 2019 at 11:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .