0

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

session["user_id"]

#check the way of request
if request.method == "GET":
    return render_template("buy.html")

#check the way of request
elif request.method == "POST":

    #extract the symbol from form
    quoted = lookup(request.form.get("quote"))

    #vailidity of symbol
    if quoted == None:
        return apology("Invalid Symbol")

    #extracting shares
    shares = request.form.get("shares")

    if not shares:
        return apology("Enter some shares")

    #checking for the cash using id
    cash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])

    #user can afford or not
    price = quoted['price']
    if price * shares > cash:
        return apology("can't afford")

    #updating the users cash if stock buyed
    balance = cash - price
    db.execute("UPDATE users SET cash = :bal WHERE id = :id",bal = balance, id = session["user_id"] )

    #including the transaction in database
    history = db.execute("INSERT INTO history (username_id, datetime, symbol, price, share, cost) VALUES (:user, :dt, :symbol, :price, :share, :costs)", user = session["user_id"], dt = datetime.now(), symbol = quoted['symbol'], price = usd(quoted['price']), share = shares, costs = cost)

    return render_template(url_for("index"))
2
  • That's probably the wrong part of the error. But there's only one * in there, so I guess it's the if price * shares > cash: line – Blauelf Oct 26 '17 at 16:00
  • So what can I do for it. – Abhishek Kaushal Oct 28 '17 at 6:35
2

I think the line

if price * shares > cash:

might be problematic on multiple stages.

Here, price would be the float, and shares the sequence.

shares is probably a string and needs conversion to integer first (also reject it if it contains any non-digit)

cash is a list of dicts, most likely containing exactly one dict with one value. The value you want is probably at cash[0]["cash"].

Your app is in debug mode. Within werkzeug's debug mode, you can open a python console at where the crash happened, printing the variables and experiencing some surprise from time to time.

3
  • How to convert the share to int if I use type conversion then the error shown is "for conversion argument must be byte or float but the passed argument is of list type". Why? – Abhishek Kaushal Oct 26 '17 at 17:06
  • @AbhishekKaushai just int(shares) – juanli Oct 26 '17 at 18:32
  • @AbhishekKaushal For instance, you fill the form with shares = 10, and the resulted shares returned by shares = request.form.get("shares") will become string '10', so you need to convert '10' to 10 using int() function. – juanli Oct 26 '17 at 18:35

You must log in to answer this question.

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