File "/home/ubuntu/workspace/pset7/finance/helpers.py", line 34, in decorated_function return f(*args, **kwargs) File "/home/ubuntu/workspace/pset7/finance/application.py", line 143, in buy name=info["name"], shares=shares, price=usd(info["price"]), \ KeyError: 'name' INFO:werkzeug: - - [11/Jul/2018 07:07:59] "POST /buy HTTP/1.1" 500 -

def buy():

if request.method == "GET":
    return render_template("buy.html")
    info = lookup(request.form.get("symbol"))

    #validate symbol entered

    if not info :
        return apology("invalid symbol")

    shares = int(request.form.get("shares"))

#validate no. shares

    if shares < 0:
      return apology("invalid stock entry", 403)

#validate cash in hand

    cash = db.execute("SELECT cash FROM users where id = :id", id = session["user_id"])
    if not cash or float(cash[0]["cash"]) < info["price"] * shares :
        return apology("insuficient cash")

#decrese the cash in hand

    db.execute("UPDATE users SET cash = cash - :purchase WHERE id = :id", id=session["user_id"], \
                    purchase=float(info["price"] * shares))

# from here onwards i got the idea from source:endiliey on github

    db.execute("INSERT INTO history (symbol, shares, price, id) VALUES(:symbol, :shares, :price, :id)", \
                symbol=info["symbol"], shares=shares, price=usd(info["price"]), id=session["user_id"])

# update user cash

    db.execute("UPDATE users SET cash = cash - :purchase WHERE id = :id", \
                id=session["user_id"], \
                purchase=info["price"] * float(shares))

# Select user shares of that symbol

    user_shares = db.execute("SELECT shares FROM transactions \
                       WHERE id = :id AND symbol=:symbol", \
                       id=session["user_id"], symbol=info["symbol"])

#validate shares in hand

    if not user_shares:
        db.execute("INSERT INTO transactions (name, shares, price, total, symbol, id) \
                    VALUES(:name, :shares, :price, :total, :symbol, :id)", \
                    name=info["name"], shares=shares, price=usd(info["price"]), \
                    total=usd(shares * info["price"]), \
                    symbol=info["symbol"], id=session["user_id"])

# Else increment the shares count

        shares_total = user_shares[0]["shares"] + shares
        db.execute("UPDATE transactions SET shares=:shares \
                    WHERE id=:id AND symbol=:symbol", \
                    shares=shares_total, id=session["user_id"], \

# return to index
    return redirect("/")

From your error message:

line 143, in buy name=info["name"], ... \ KeyError: 'name' 

The issue appears to be the 'KeyError' 'name':


Does your dictionary 'info' have a key called name?

You must log in to answer this question.

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