0

I ran check50 to check some things that were wrong and I got 2 errors but when i check the details of the error it doesnt really give me a detailed overview of the problem so I am not really sure how to fix it or whats wrong with the code if anyone could help that would be great :D

edit: this is one of the errors i get now when try to buy a stock. I didnt have this issue before...

File "/home/ubuntu/pset9/finance/application.py", line 106, in buy amount = shares * price TypeError: can't multiply sequence by non-int of type 'float'

error 1

error 2

this is my code for the buy function where the errors are I am not sure which line the error is

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
# checks if method is POST
if request.method == "POST":
    # request from the the form
    shares = request.form.get("shares")
    quote = lookup(request.form.get("symbol"))
    symbol = request.form.get("symbol")
    stocks = lookup(symbol)
    price = quote.get("price")

    # checks if there is no stock name
    if not shares.isdigit():
        return apology("Oops! you need to input a positive integer")

    if not quote:
        return apology("Oops! you forogot the stock name")
    # checks if there is no share quantity
    elif not shares:
        return apology("Oops! you are missing the share quantity")
    # checks if the symbol is invaild
    if stocks is None:
        return apology("Oops! Invalid Symbol")

    # selects users from the users table
    rows = db.execute("SELECT cash FROM users WHERE id=:id", id=session["user_id"])

    # calculation for new total
    cash = rows[0]["cash"]
    amount = (shares * price)
    new_total = cash - amount

    # checks if you can afford
    if new_total < 1:
        return apology("Oops! you cant afford")

    if db.execute("SELECT symbol,user_id FROM user_log WHERE user_id=:user_id AND symbol = :symbol", user_id=session["user_id"], symbol=symbol):
        current_share=db.execute("SELECT share_amount FROM user_log WHERE user_id=:user_id AND symbol = :symbol", user_id=session["user_id"], symbol=symbol)
        share_index=current_share[0]["share_amount"]
        new_share_amount = share_index + shares
        share_amount=shares
        db.execute("UPDATE user_log SET share_amount=:share_amount WHERE user_id=:user_id AND symbol=:symbol",share_amount=new_share_amount, user_id=session["user_id"],symbol=symbol)
    elif request.method == "GET":
        # cursor = connection.cursor()
        insert = """INSERT INTO user_log (user_id, symbol, share_amount) VALUES (%s) """
        record = (session["user_id"], symbol, shares)
        db.execute(insert, record)

    # inserts the share,symbol,price into the tranactions table
    db.execute("INSERT INTO transactions (shares, symbol, price, user_id, transaction_type) VALUES (:shares, :symbol, :price, :user_id, 'buy')",shares=shares, symbol=symbol, price=price, user_id = session["user_id"])

    # updates the cash of the user
    db.execute("UPDATE users SET cash=:new_total WHERE id=:id", new_total=new_total, id=session["user_id"])
    return redirect("/")
else:
    return render_template("buy.html")
1

Quote and stocks are one and the same. So you should keep only one of them. Your first error takes place at price = quote.get("price"). This line is correct but in the wrong place. Once you have checked if quote is not None, you should get the price, not before checking. This should resolve your first error. As your code shows, you know that shares is a string. You can't multiply a string with a float. In this case, you should change amount = (shares * price) to amount = (int(shares) * price). This should solve your second problem.

Hope this helps. If it does then please check the tickmark.

3
  • Hi thank you for your solution it worked very well but now there is a new error and I am not sure what it means buy handles valid purchase Cause expected to find "112.00" in page, but it wasn't found Log sending POST request to /login sending POST request to /buy checking that "112.00" is in page
    – Arsham
    Mar 18 at 5:44
  • Please ask this as a new question
    – Vsjain
    Mar 18 at 6:02
  • I have now asked it as a question
    – Arsham
    Mar 18 at 6:59

You must log in to answer this question.

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