def index():
    """Show portfolio of stocks"""
    cash = db.execute('SELECT cash FROM users where id=:id',id=session["user_id"])
    cash = cash[0]["cash"]
    stocks = db.execute('SELECT * FROM portfolio where id=:id',id=session["user_id"])
    total_s = 0
    for stock in stocks:
        quote = lookup(stock['symbol'])
        shareV = quote['price']*int(stock['number'])
        positions = {'symbol':quote['symbol'],'name':quote['name'],'price':quote['price'],'number':stock['number'],'share_value':shareV}
    total = total_s+cash
    return render_template("index.html", stocks=stocks,cash=cash,totals=total_s,total=total)
@app.route("/buy", methods=["GET", "POST"])
def buy():
    """Buy shares of stock"""
    if request.method == "POST":
        if not request.form.get("symbol"):
            return apology("no such stock exists")
        elif not request.form.get("number"):
            return apology("wrong number of shares")
        a = request.form.get("symbol")
        b = int(request.form.get("number"))
        cash = db.execute('SELECT cash FROM users WHERE id = :id',id = session["user_id"])
        cash = float(cash[0]["cash"])
        quote = lookup(a)
        if not quote:
            return apology("no such stock exists")
        elif float(quote["price"]*b)>cash:
            return apology("budget over")
        db.execute("UPDATE users SET cash = cash-:c where id=:id",id = session["user_id"],c = quote["price"]*b)
        k=db.execute("SELECT number FROM portfolio WHERE id=:id AND symbol = :symbol",id = session["user_id"],symbol = quote['symbol'])
        if not k:
            db.execute("INSERT INTO portfolio (symbol,name,number,id) VALUES(:a,:name,:b,:id)",a = quote['symbol'],name=quote['name'],b = b,id = session['user_id'] )
            db.execute("UPDATE portfolio SET number=:number WHERE id = :id AND symbol = :symbol",number = t,id = session["user_id"],symbol = quote['symbol'])
        return redirect("/")
        return render_template("buy.html")

enter image description hereenter image description here enter image description here

Hello! I am on the verge of giving up this homework due to this buy function error... I cannot see why my code doesnt work=( is there anyone who can tell me what is the problem causing the error 'expected to get 112.00 in page but not found'? Editing render_template arguments was not the right solution..I think the use of library is problem..but still don't know exactly what causes the error.


Would have been incredibly helpful to have the relevant line number in your code, as presented in complete stack trace. This way, I can only guess what caused it.

Remember that SELECT returns a list of dicts. Instead of

t = k + b


t = k[0]['number'] + b

(and style50 also works for Python files...)

edit: Seems like that was a problem, but not the only one: positions got used in the template, but never passed to the template.

  • I don't get 112.00 in the page... that is the error. if this is what you mean. " expected to find "112.00" in page, but it wasn't found " so I assumed it is related to the index function..(still buy function can be a source of problem though..)
    – dbdbdbdb
    May 14 '18 at 12:24
  • Then state that in your question! Which template is your first screenshot? Where do you define positions in your render_template call?
    – Blauelf
    May 14 '18 at 12:46
  • Oh, and remove usd() from your shareV = line. It looks weird to apply it twice.
    – Blauelf
    May 14 '18 at 12:49
  • I am sorry..it is my first time to ask a question in this site?community?...the last screenshot is index function coding. I defined positions there as a dictionary but no I didn't define positions!! omg! thank you very much T-T..but it doesnt work...
    – dbdbdbdb
    May 14 '18 at 12:50
  • Please for any question, give us relevant information. Like the problem itself. You say "above error", but you don't state any. And your screenshots don't all tell what we see. And have you tried the {} button in the editor? It turns copied code into a code block (by adding four spaces in front of all the selected lines). That's much more readable than screenshots.
    – Blauelf
    May 14 '18 at 12:54

You must log in to answer this question.

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